correct the distance function