     uint8_t* const d_dest
 ) {    
     // get some segment (size of threadblocks is 32 x N, so threadIdx.y is warp index)
-    const int segment_idx = threadIdx.y + blockIdx.x * blockDim.y;
+    const int block_idx = blockIdx.x + blockIdx.y * gridDim.x;
+    const int segment_idx = threadIdx.y + block_idx * blockDim.y;
     if(segment_idx >= segment_count) {
         return;
     }

    return 0;
}

/** Documented at declaration */
int
gpujpeg_huffman_gpu_encoder_encode(struct gpujpeg_encoder* encoder, unsigned int * output_byte_count)

    
    // Run output compaction kernel (one warp per segment)
    const dim3 compaction_thread(32, WARPS_NUM);
    const dim3 compaction_grid = gpujpeg_huffman_gpu_encoder_grid_size(gpujpeg_div_and_round_up(coder->segment_count, WARPS_NUM));
    gpujpeg_huffman_encoder_compaction_kernel<<<compaction_grid, compaction_thread>>>(
        coder->d_segment,
        coder->segment_count,
