Fixed calculation of distance