move flattenTerms before ResolveBuiltins