     // last thread handles special block-termination symbol
     if(0 == ((tid ^ 31) | in_odd)) {
         // this causes selection of huffman symbol at index 256 (which contains the termination symbol)
-        zeros_before_odd = 16;
+        zeros_before_odd = 256;
     }
     
     // each thread gets codeword for its two pixels

    const unsigned int value_code = packed_value & ~0xf;
    
    // find prefix of the codeword and size of the prefix
    const int prefix_idx = preceding_zero_count * 16 + value_nbits;
    const unsigned int prefix_code = d_table->gcode[prefix_idx];
    const unsigned int prefix_nbits = prefix_code & 31;
    
    // compose packed codeword with its size

    int in_even = block[gpujpeg_huffman_gpu_encoder_order_natural[load_idx]];
    const int in_odd = block[gpujpeg_huffman_gpu_encoder_order_natural[load_idx + 1]];
    
    // compute preceding zero count for even coefficient (actually compute the count multiplied by 16)
    const unsigned int nonzero_mask = (1 << tid) - 1;
    const unsigned int nonzero_bitmap_0 = 1 | __ballot(in_even);  // DC is always treated as nonzero
    const unsigned int nonzero_bitmap_1 = __ballot(in_odd);
    const unsigned int nonzero_bitmap_pairs = nonzero_bitmap_0 | nonzero_bitmap_1;
    
    const int zero_pair_count = __clz(nonzero_bitmap_pairs & nonzero_mask);
    int zeros_before_even = 32 * (zero_pair_count + tid - 32);
    if((0x80000000 >> zero_pair_count) > (nonzero_bitmap_1 & nonzero_mask)) {
        zeros_before_even += 16;
    }
    
    // true if any nonzero pixel follows thread's odd pixel
