Now using distance function
distance function used.