Factored coordinate comparing code to separate function