   float *gradInput, const float *indice, const float *gradOutput, 
   int inputSize, int outputSize)
 {
-  __shared__ float buffer[LAZYKBEST_THREADS];
   int tx = threadIdx.x;
   int step = blockDim.x;
   int k = blockIdx.x;
   
   float *gradInput_k = gradInput + k*inputSize;
   const float *gradOutput_k = gradOutput + k*outputSize;
-  const float *indice_k = output + k*outputSize;
+  const float *indice_k = indice + k*outputSize;
   
   for (int i=tx; i<outputSize; i+=step)
-    gradInput_k[(int)(indice[i])] = gradOutput[i];
+    gradInput_k[(int)(indice_k[i])] = gradOutput_k[i];
 }
 
 

  const int k = blockIdx.x;
  
  float *output_k = output + k*outputSize;
  float *indice_k = input + k*outputSize;
  const float *input_k = input + k*inputSize;
  
  float maxVal = -FLT_MAX;


  luaL_argcheck(L, input->nDimension == 2, 2, "2D(batch mode) tensor expected");
  luaL_argcheck(L, indice->nDimension == 2, 3, "2D(batch mode) tensor expected");
  luaL_argcheck(L, k <= LAZYKBEST_THREADS, 1, "k must be smaller than KBEST_THREADS");
  luaL_argcheck(L, THCudaTensor_isContiguous(input), 2, "Expecting contiguous input");
  
  THCudaTensor_resize2d(output, input->size[0], k);
