Adding a distance function