Extract calculateDistance function out