Merged changes to calculate distance.