           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::vector<float> dist_ndx(n_rows * 2);
      auto update_nh = [&dist_ndx,n_rows] (Neighborhood& _nh) -> void {
        for (unsigned int i=0; i < n_rows; ++i) {
          if (dist_ndx[i] < _nh[i].second && dist_ndx[i] != 0) {
            _nh[i] = {(unsigned int) dist_ndx[n_rows+i]
                    , dist_ndx[i]};
          }
