Complete rewrite of Tree to improve structure