move flattenTerms before ResolveRewrite (back)