Working treeFromList function