Fix tree building bug