Refactor tree generator, increase code reuse