Distance function is now working