big refactoring for tree structure.