                                                         type: syntax.MemberExpression,
                                                         computed: false,
                                                         object: create_identifier env_name
-                                                        property: create_identifier param.name
-                                                right: create_identifier param.name
+                                                        property: create_identifier "%env_#{n.ejs_env.parent.id}"
+                                                right: create_identifier "%env_#{n.ejs_env.parent.id}"
                                 }
-
-                new_mapping = deep_copy_object @currentMapping()
-
-                flatten_memberexp = (exp) ->
-                        if exp.type isnt syntax.MemberExpression
-                                [exp]
-                        else
-                                (flatten_memberexp exp.object).concat [exp.property]
-
-                prepend_environment = (exps) ->
-                        obj = create_identifier env_name
-                        for prop in exps
-                                obj = {
+                                
+
+                        # we need to push assignments of any closed over parameters into the environment at this point
+                        for param in n.params
+                                if n.ejs_env.closed.member param.name
+                                        n.body.body.splice 1, 0, {
+                                                type: syntax.ExpressionStatement
+                                                expression:
+                                                        type: syntax.AssignmentExpression,
+                                                        operator: "="
+                                                        left:
+                                                                type: syntax.MemberExpression,
+                                                                computed: false,
+                                                                object: create_identifier env_name
+                                                                property: create_identifier param.name
+                                                        right: create_identifier param.name
+                                        }
+
+                        new_mapping = deep_copy_object @currentMapping()
+
+                        flatten_memberexp = (exp) ->
+                                if exp.type isnt syntax.MemberExpression
+                                        [exp]
+                                else
+                                        (flatten_memberexp exp.object).concat [exp.property]
+
+                        prepend_environment = (exps) ->
+                                obj = create_identifier env_name
+                                for prop in exps
+                                        obj = {
+                                                type: syntax.MemberExpression,
+                                                computed: false,
+                                                object: obj
+                                                property: prop
+                                        }
+                                obj
+
+                        # if there are existing mappings prepend "%env." (a MemberExpression) to them
+                        for mapped of new_mapping
+                                val = new_mapping[mapped]
+                                new_mapping[mapped] = prepend_environment flatten_memberexp val
+                        
+                        # add mappings for all variables in .closed from "x" to "%env.x"
+
+                        new_mapping["%env"] = create_identifier env_name
+                        n.ejs_env.closed.map (sym) ->
+                                new_mapping[sym] =
                                         type: syntax.MemberExpression,
                                         computed: false,
-                                        object: obj
-                                        property: prop
-                                }
-                        obj
-
-                # if there are existing mappings prepend "%env." (a MemberExpression) to them
-                for mapped of new_mapping
-                        val = new_mapping[mapped]
-                        new_mapping[mapped] = prepend_environment flatten_memberexp val
-                
-                # add mappings for all variables in .closed from "x" to "%env.x"
-
-                new_mapping["%env"] = create_identifier env_name
-                n.ejs_env.closed.map (sym) ->
-                        new_mapping[sym] =
-                                type: syntax.MemberExpression,
-                                computed: false,
-                                object: create_identifier env_name
-                                property: create_identifier sym
-
-                @mappings.unshift new_mapping
-                super
-                @mappings = @mappings.slice(1)
+                                        object: create_identifier env_name
+                                        property: create_identifier sym
+
+                        @mappings.unshift new_mapping
+                        super
+                        @mappings = @mappings.slice(1)
 
                 # we need to replace function declarations of the form:
                 #   function X () { ...body... }


        debug.log "after LocateEnvVisitor:"
        debug.log -> escodegen.generate tree3
        
        substitute_vars = new SubstituteVariables tree3
        tree4 = substitute_vars.visit tree3


                rv                        
                
        visitFunction: (n) ->
                if n.ejs_env.closed.empty()
                        n.body.body.unshift
                                type: syntax.VariableDeclaration,
