changed distance function