Fix the sorting function