Changed distance function