Fixing bug with sorting