fine tune the sorting function