Added distance calculation and sorting