rewrote tree, more generic approach