Refactor sorting a little.