Simplified the logic that sets up the sorting code