improve nearest_ij()