refactor sorting