Transform/flatten

Two more miniphases: Flatten and RestoreScopes.