Change distance algorithm for slight performance boost