   dim3 blocks(16, 1, 1);
   dim3 threads(BitonicSortThreads, 1, 1);
   bitonicSort << <blocks, threads>>> (pts, newpts);
-  cudaDeviceSynchronize();
-  safeCall(cudaGetLastError());
-
-  cv::KeyPoint* newpts_h = new cv::KeyPoint[nump];
+
+  /*unsigned int extremaidx_h[16];
+  cudaMemcpyFromSymbol(extremaidx_h,d_ExtremaIdx,16*sizeof(unsigned int));
+  for (int i=0; i<16; ++i) {
+      std::cout << i << ": " << extremaidx_h[i] << std::endl;
+  }
+  */
+  
+/*  cv::KeyPoint* newpts_h = new cv::KeyPoint[nump];
   cudaMemcpy(newpts_h,newpts,nump*sizeof(cv::KeyPoint),cudaMemcpyDeviceToHost);
 
-  /*for (int i=0; i<nump; ++i) {
-      std::cout << i << ": " << newpts_h[i].pt.y << " " << newpts_h[i].pt.x << std::endl;
-      }*/
-  
-  // Find all neighbors
+  int scale = 0;
+  for (int i=1; i<nump; ++i) {
+      cv::KeyPoint &k0 = newpts_h[i-1];
+      cv::KeyPoint &k1 = newpts_h[i];
+
+      std::cout << i << ": " << newpts_h[i].class_id << ": " << newpts_h[i].pt.y << " " << newpts_h[i].pt.x << ", " << newpts_h[i].size;
+
+      if (!(k0.pt.y<k1.pt.y || (k0.pt.y==k1.pt.y && k0.pt.x<k1.pt.x))) std::cout << "  <<<<";
+      if (k1.size < 0 ) std::cout << "  ##############";
+       
+
+      std::cout << "\n";
+
+  }
+*/
+
+    // Find all neighbors
   cudaStreamSynchronize(copyStream);
-  blocks.x = 1024;//nump;
+  blocks.x = nump;
   threads.x = FindNeighborsThreads;
   FindNeighbors << <blocks, threads>>> (newpts, kptindices, width);
-  cudaDeviceSynchronize();
-  safeCall(cudaGetLastError());
+  //cudaDeviceSynchronize();
+  //safeCall(cudaGetLastError());
   
   // Filter extrema
   blocks.x = 1;

  cudaMalloc((void**)&buffer3, nump);
  FilterExtrema_kernel << <blocks, threads>>> (newpts, pts, kptindices, width,
					       buffer1, buffer2, buffer3);
  cudaDeviceSynchronize();
  safeCall(cudaGetLastError());
  cudaFree(buffer1);
  cudaFree(buffer2);
  cudaFree(buffer3);


  for (int i = 0; i < desc_query.rows; ++i) {
    std::vector<cv::DMatch> tdmatch;
    //std::cout << dmatches_h[2*i].trainIdx << " - " << dmatches_h[2*i].queryIdx << std::endl;
    tdmatch.push_back(dmatches_h[2 * i]);
    tdmatch.push_back(dmatches_h[2 * i + 1]);
    dmatches.push_back(tdmatch);
