small changes to distance function