simplified the sort function