         // Encode MCUs in segment
         for ( int mcu_index = 0; mcu_index < segment->mcu_count; mcu_index++ ) {
             // Encode 8x8 block
-            if (gpujpeg_huffman_gpu_encoder_encode_block(block, data_compressed, s_in, s_out, out_size, &last_dc, tid, d_table_dc, d_table_ac) != 0)
+            if (gpujpeg_huffman_gpu_encoder_encode_block(block, data_compressed, s_temp, s_out, remaining_bits, remaining_bytes, &last_dc, tid, d_table_dc, d_table_ac) != 0)
                 break;
             block += component->mcu_size;
         }

 * @return 0 if succeeds, otherwise nonzero
 */
__device__ int
gpujpeg_huffman_gpu_encoder_encode_block(int16_t * block, uint8_t * &data_compressed, int * s_in, int * s_out, int &out_size, int *last_dc, int tid,
                struct gpujpeg_table_huffman_encoder* d_table_dc, struct gpujpeg_table_huffman_encoder* d_table_ac)
{
    // each thread loads a pair of values (pair after zigzag reordering)

    
    int warpidx  = threadIdx.x >> 5;
    int tid    = threadIdx.x & 31;
    int remaining_bytes = 0;
    int remaining_bits = 0;

    __shared__ int s_temp_all[64 * WARPS_NUM];
    __shared__ int s_out_all[192 * WARPS_NUM];

    int * s_temp = s_temp_all + warpidx * 64;
    int * s_out  =  s_out_all + warpidx * 192;
    
    // Select Segment
    int segment_index = blockIdx.x * WARPS_NUM + warpidx;
