cleaned up findMinDistance