Distance function rework