         this
 
 
+
 class Builder extends EventEmitter
     constructor: (@opts = {}) ->
+        @builder = this
         @buffer = [] # for child output
         @closed = no
         @pending = [] # no open child tag
-        @opts.Tag ?= Tag
+        @opts.builder ?= this
         @opts.pretty ?= off
         @level = @opts.level ? 0
         @Tag = @opts.Tag or Tag
 
-    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) =>
+        # sync tag, - same as normal tag, but closes it automaticly
+        unless typeof attrs is 'object'
+            [opts, children, attrs] = [children, attrs, {}]
+        else
+            attrs ?= {}
+        opts ?= {}
+        self_ending_children_scope = ->
+            @children children
+            @end()
+        @_new_tag parent, name, attrs, self_ending_children_scope, opts
+
+    tag: (args...) =>
         @level--
-        tag = new_tag.apply this, arguments
+        tag = @_new_tag.apply this, [this].concat args
         @level++
         tag
 
     $tag: (args...) =>
-        # sync tag, - same as normal tag, but closes it automaticly
-        sync_tag.apply this, arguments
+        @level--
+        tag = @_new_sync_tag.apply this, [this].concat args
+        @level++
+        tag
 
     end: (data) =>
         if @pending.length

        attrs ?= {}
    opts ?= {}
    pipe = {}
    opts.level = @level+1
    opts = deep_merge @opts, opts # possibility to overwrite existing opts, like pretty


    @pending.push tag = new @Tag name, attrs, _children, opts

    tag.up = (opts = {}) => # set parent
        opts.end ?= true

        if @headers
            @emit 'data', "#{indent this}#{@headers}>"
            delete @headers
        @emit 'data', "#{indent this}#{content}" if content # FIXME is this really nessary?
        @content += content
        true

    up: () -> @builder # this node has no parent

    end: () =>
        if not @closed or @closed is 'pending'
