 
     write: (content, {escape} = {}) =>
         content = safe(content) if escape
-        if @headers
-            @emit 'data', "#{indent this}#{@headers}>"
-            delete @headers
-        @emit 'data', "#{indent this}#{content}" if content # FIXME is this really nessary?
-        @content += content
+        if @isempty
+            @emit 'data', "#{indent this}<#{@name}#{new_attrs @attrs}>"
+            @isempty = no
+        @emit 'data', "#{indent this}#{content}" if content
         true
 
     up: () -> @builder # this node has no parent
 
     end: () =>
         if not @closed or @closed is 'pending'
-            if @headers
-                data = "#{indent this}#{@headers}/>"
-                @closed = 'self'
+            if @pending.length
+                @emit 'close', this if @closed isnt 'pending'
+                @closed = 'pending'
             else
-                data = "#{indent this}</#{@name}>"
-                if @pending.length
-                    @closed = 'pending'
+                if @isempty
+                    data = "#{indent this}<#{@name}#{new_attrs @attrs}/>"
+                    @closed = 'self'
                 else
+                    data = "#{indent this}</#{@name}>"
                     @closed = yes
-            @emit 'close', this
-            @emit 'end', data unless @closed is 'pending'
+                @emit 'data', data
+
+            @emit 'end' unless @closed is 'pending'
         else if @closed is 'removed'
             @emit 'end'
         else

        @Tag = @opts.Tag or Tag

    _new_tag: (parent, args...) =>
        if parent.headers
            parent.emit 'data', "#{indent parent}#{parent.headers}>"
            delete parent.headers
        new_tag.apply parent, [this].concat args

    _new_sync_tag: (parent, name, attrs, children, opts) =>

        @pending = [] # no open child tag
        @closed = false
        @writable = true
        @isempty = yes
        @content = ""
        @children children, @opts

    tag: (args...) =>
