fix the bug of calculation of distance