Fixing sort function