working version / automated tree building