Refactor how the tree is recursivly build