Sorting bug fixes