Rewrite the entire buildTree method.

Test tomorrow. Hopefully it works!