Replace repeated code with references to SortRoots.