 
         // Write Category offset
         if ( gpujpeg_huffman_gpu_encoder_emit_bits((unsigned int) temp2, nbits, put_value, put_bits, data_compressed) != 0 )
-            return -1;
-    }
-
-    // If all the left coefs were zero, emit an end-of-block code
-    if ( last_nonzero_idx != 63 ) {
-        if ( gpujpeg_huffman_gpu_encoder_emit_bits(d_table_ac->code[256], d_table_ac->size[256], put_value, put_bits, data_compressed) != 0 )
             return -1;
     }
 

    ((short2*)s_in)[load_idx].y = zeros_before_even & 0xF;
    ((short2*)s_in)[load_idx + 1].x = in_odd;
    ((short2*)s_in)[load_idx + 1].y = zeros_before_odd & 0xF;
    __threadfence_block();
    
    

    if ( gpujpeg_huffman_gpu_encoder_emit_bits((unsigned int) temp2, nbits, put_value, put_bits, data_compressed) != 0 )
        return -1;
    
//     // find last nonzero value
//     int last_nonzero_idx = 64;
//     while( last_nonzero_idx-- ) {
//         if( data[last_nonzero_idx].x ) {
//             break;
//         }
//     }
    
    // Encode the AC coefficients per section F.1.2.2 (r = run length of zeros)
    for ( int k = 1; k < 64; k++ ) 
    {
        const int value = temp2 = temp = data[k].x;
        const int r = data[k].y;

        // if last coefficient is zero
        if ( r == -1 ) {
            if ( gpujpeg_huffman_gpu_encoder_emit_bits(d_table_ac->code[256], d_table_ac->size[256], put_value, put_bits, data_compressed) != 0 )
                return -1;
            break;
        }
        
        if ( temp < 0 ) {
            // temp is abs value of input
            temp = -temp;        
