refactor sorting
move sorting out into a separate method
remove unused variables, including locations array