implemented nearest neighbor search