 
   // Save the sum at the appropriate position in the diff vector.
   if (tix == 0)// && data_ind < N*D) 
-    dist[threadIdx.y + blockIdx.x*blockDim.y + N*blockIdx.y + offset*MAX_DATA_POINTS] = Z[tix][tiy]; //+ N*blockIdx.y
+    dist[threadIdx.y + blockIdx.x*blockDim.y+ MAX_DATA_POINTS*blockIdx.y] 
+          = Z[tix][tiy];
 
 }
 

  // Index in current grid line (y-dimesnion) for every query computation                          
  unsigned int query_ind = threadIdx.x + blockIdx.y*D;

/*
  unsigned int i = blockIdx.x*blockDim.x*blockDim.y + 
                   threadIdx.y*blockDim.x + threadIdx.x;
*/

  // Save the difference in the appropriate possition.
  double tmp = 0;

*/  


  // Only for debugging purposes.
/*
  int max_qi = Q;
  // if N is greater than 10K, then print only the first 10K elements
  int max_i = (N > 10000) ? 10000 : N; 
  
  for (int qi = 0; qi < max_qi; qi++) {
    for (int i = 0; i < max_i; i++) {  
      printf("qi = %d, i = %d, dist = %f\n", qi, i, dist[qi*N + i]);
    }
  }
*/

  cudaFree(deviceData); 
  cudaFree(deviceQueries);
