Moves tree opening and manipulation into a function.