Changed the logic of distance