Adding distance function