Initial implementation of tree construction

Possibly awful.