Improve sort functionality