Working on distance function