Revert "Refactor to use SortRoots function"

This reverts commit 57013df1f8e30a85ce4c4b675739e0eb2b51d28b.