Attempt to improve distance function.