fixing sorting code