                     @buffer = @buffer.concat tag.buffer
                     tag.buffer = []
                 @pending = @pending.slice(1)
-                tag.removeListener 'data', pipe.data
-                for event in EVENTS
-                    tag.removeListener event, pipe[event]
-                if @buffer.length
-                    for data in @buffer
-                        @write data
-                    @buffer = []
+                rmevents.call tag, events
+                flush.call this
                 if @closed and @pending.length is 0
                     @end()
         else

        tag.end.apply tag, arguments if opts.end
        this

    tag.on 'data', pipe.data = (data) =>
        if @pending[0] is tag
            @write data
        else
            @buffer.push data

    EVENTS.forEach (event) =>
        tag.on event, pipe[event] = (args...) =>
            @emit event, args...

    tag.on 'end', on_end = =>
        tag.removeListener 'end', on_end
        if @pending[0] is tag
            if tag.pending.length
                tag.pending[0].once 'end', on_end

    @emit 'add', tag
    tag.children children, opts if children?
    return tag

sync_tag = ->
    [name, attrs, children, opts] = parse_args arguments...
