added the main distance function