Fixing improper tree generation in certain cases