added totalDistance function