Fixing sorting bug