Optimize getDistance function as getDistance_alt1