BF: corrected distance function