Refactored distance stuff