some fixes and approvment for sorting