proper sorting
some fixes