Refactored distance to use arrays only