fixed bugs added sort function