move flattenTerms before ResolveFreshVarMOS