implement nearest and inearest