Fix distance calculation