Cleans up tree-building code.