Extract distance computation logic into private method

Rather than duplicating computation logic, move to private method
computeDistance. Also move all pattern matching to getXYVals
method to simplify apply() method logic