refactoring to save memory.
tree is not correct