Bug in computing the distance