Move some code out of the flattenExpr to global scope.