corrected a bug with sorting.