Corrected bug in total distance calculation