changed sorting method nd fixed recurssion