Changed treebuilding alg