Done second half. TODO: buildtree()