           nh_minndx = i+offset;
         }
         // frame with min distance and lower energy
-        if ((nhhd_mindist == 0)
+        if ((nhhd_mindist == 0 && fe[i+offset] < ref_fe)
          || (dist2 < nhhd_mindist && fe[i+offset] < ref_fe && dist2 != 0)) {
           nhhd_mindist = dist2;
+          //TODO: still error with indices: some results are > n_rows !
           nhhd_minndx = i+offset;
         }
       }

      std::cerr << "error: max. shared mem per block too small on this GPU.\n"
                << "       either reduce block_size for NN search or get a "
                <<        "better GPU." << std::endl;
    }
    unsigned int n_rows_ext = min_multiplicator(n_rows
                                              , block_size)

      std::cerr << "error: no CUDA-compatible GPUs found" << std::endl;
      exit(EXIT_FAILURE);
    }

//TODO 
std::cout << "n_gpus: " << n_gpus << std::endl;

    std::vector<std::tuple<Neighborhood, Neighborhood>> partials(n_gpus);
    unsigned int gpu_range = n_rows / n_gpus;
    unsigned int i_gpu;
