Move function for building tree to module