Modularized the distance calc