Sorting and minor cleanup