change flattenTerms and addInjections and move FlattenTerms before AddInjections