Added more advanced version of emitTree, still not quite there though