                             console.log(e.stack)
                             log("BUG: error rendering output: '#{mesg}' -- #{e}")
 
-                    if mark? and not mark.finish_editing? and mark.processed != x
+                    output = @elt_at_mark(mark)
+                    if mark? and output? and not mark.finish_editing? and mark.processed != x
                         # This is more complicated than you might think because past messages can
                         # be modified at any time -- it's not a linear stream.
-                        output = @elt_at_mark(mark)
                         # appearance of output shows output
                         output.removeClass('sagews-output-hide')
                         messages = x.split(MARKERS.output).slice(2) # skip output uuid

                        @close_on_action(elt)

    elt_at_mark: (mark) =>
        elt = mark.replacedWith
        if elt?
            return $(elt)


                    if marks.length == 0
                        @mark_output_line(cm, line)
                    mark = cm.findMarksAt({line:line, ch:1})[0]
                    f = (elt, mesg) =>
                        try
                            @process_output_mesg
