Fixed sorting bugs