Improve tree generation

And many other changes. Kinda forgot to commit for a while. dammit.