Bug fix: may generate incorrect number of trees