Corrected distance function