Separate calculating distance functionality to a function