                 #
                 #    makeClosure(%current_env, "X", function () { ...body... })
 
-                if n.ejs_env.parent
+                if not n.toplevel
                         if n.type is syntax.FunctionDeclaration
                                 throw "there should be no FunctionDeclarations at this point"
                         else # n.type is syntax.FunctionExpression
                                 call_exp =
                                         type: syntax.CallExpression,
                                         callee: create_identifier "%makeClosure"
-                                        arguments: [ (create_identifier "%env_#{n.ejs_env.parent.id}"), (create_string_literal if n.id then n.id.name else ""), n ]
+                                        arguments: [ (if n.ejs_env.parent? then (create_identifier "%env_#{n.ejs_env.parent.id}") else { type: syntax.Literal, value: null}), (create_string_literal if n.id then n.id.name else ""), n ]
                                 return call_exp
                 n        
                 

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

                                                        type: syntax.MemberExpression,
                                                        computed: false,
                                                        object: create_identifier env_name
                                                        property: create_identifier "%env_#{n.ejs_env.parent.id}"
                                                right: create_identifier "%env_#{n.ejs_env.parent.id}"
                                }
                                

                        # 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: 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... }
