                                     console.warn("invalid output message '#{m}' in line '#{line}'")
                                     continue
 
-                                if DEBUG then console.log 'sagews2html: output message', mesg
+                                #if DEBUG then console.log 'sagews2html: output message', mesg
                                 if mesg.stdout?
-                                    mesg_stdout.stdout += mesg.stdout
+                                    mesg_stdout.stdout.push(mesg.stdout)
                                 else
                                     # add non-stdout mesg from this iteration
                                     if mesg.clear
-                                        mesg_stdout = {stdout : ''}
+                                        mesg_stdout = {stdout : []}
                                         output_msgs = []
                                     else if mesg.delete_last
-                                        output_msgs.pop()
+                                        mesg_stdout.stdout.pop()
                                     else
                                         output_msgs.push(mesg)
-                                    #process_collected_mesg_stdout()
                             # after for-loop over rendered output cells
                             for mesg in output_msgs
                                 om = @html_process_output_mesg(mesg, mark)

            input_lines_mode = null

        # stdout mesg can be split up into multiple parts -- this is a helper for collecting them
        mesg_stdout = {stdout : ''}
        process_collected_mesg_stdout = =>
            # processing leftover stdout mesgs from previous iteration
            if mesg_stdout.stdout.length > 0
                # it's ok to leave `mark` undefined
                om = @html_process_output_mesg(mesg_stdout)
                _html.push(om) if om?
                mesg_stdout = {stdout : ''}

        process_lines = (cb) =>
            # process lines in an async loop to avoid blocking on large documents

    html_process_output_mesg: (mesg, mark) ->
        # Note: each mesg could contain more than one output type
        out = ''
        # if DEBUG console.log 'html_process_output_mesg', mesg, mark
        if mesg.stdout?.length > 0
            # assertion: for stdout, `mark` might be undefined
            stdout = mesg.stdout.join('\n')
            out += "<div class='output stdout'>#{stdout}</div>"
        if mesg.stderr?
            out += "<div class='output stderr'>#{mesg.stderr}</div>"
        if mesg.html?
