Completely revamped the sorting mechanism to be cleaner and more efficient