Closest neighbor implementation