Made the tree building phase recursive.