Sorting and cleanup.