Further fixes the sort function