Improve order & treeify functions