     
     // count of consecutive zeros before odd value (either one more than 
     // even if even is zero or none if even value itself is nonzero)
-    int zeros_before_odd = in_even || !tid ? 0 : zeros_before_even + 1;
+    // (the count is actually multiplied by 16)
+    int zeros_before_odd = in_even || !tid ? 0 : zeros_before_even + 16;
     
     // clear zero counts if no nonzero pixel follows (so that no 16-zero symbols will be emited)
     // otherwise only trim extra bits from the counts of following zeros
-    const int zero_count_mask = nonzero_follows ? 0xF : 0;
+    const int zero_count_mask = nonzero_follows ? 0xF0 : 0;
     zeros_before_even &= zero_count_mask;
     zeros_before_odd &= zero_count_mask;
     

    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
    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 = 2 * (zero_pair_count + tid - 32);
    if((0x80000000 >> zero_pair_count) > (nonzero_bitmap_1 & nonzero_mask)) {
        zeros_before_even++;
    }
    
    // true if any nonzero pixel follows thread's odd pixel

    // 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 = 256;
    }
    
    // each thread gets codeword for its two pixels
