     onIOMessage: (message) ->
         console.log "IO message:", message
 
-        unless message.content?
-            console.log "onIOMessage: Missing message.content"
+        unless @_isValidMessage message
             return
 
-        msg_type = message.header?.msg_type
+        msg_type = message.header.msg_type
 
         if msg_type is 'error'
             #TODO; produces to much warning & errors, maybe filter?

                @watchCallbacks.forEach (watchCallback) ->
                    watchCallback()

        msg_id = message.parent_header?.msg_id
        if msg_id?
            callback = @executionCallbacks[msg_id]

        if callback?
            resultObject = @getResultObject message
            if resultObject?
                callback resultObject


    getResultObject: (message) ->
        msg_type = message.header?.msg_type

        if msg_type == 'pyout' or
           msg_type == 'display_data' or
           msg_type == 'execute_result'
            if message.content.data['text/html']?
                return {
                    data: message.content.data['text/html']
                    type: 'text/html'
                    stream: 'pyout'
                }
            if message.content.data['image/svg+xml']?
                return {
                    data: message.content.data['image/svg+xml']
                    type: 'image/svg+xml'
                    stream: 'pyout'
                }

            imageKeys = _.filter _.keys(message.content.data), (key) ->
                return key.startsWith('image')
            imageKey = imageKeys[0]

            if imageKey?
                return {
                    data: message.content.data[imageKey]
                    type: imageKey
                    stream: 'pyout'
                }
            else
                return {
                    data: message.content.data['text/plain']
                    type: 'text'
                    stream: 'pyout'
                }
        else if msg_type == 'stdout' or
                message.idents == 'stdout' or
                message.idents == 'stream.stdout' or
                message.content.name == 'stdout'
            return {
                data: message.content.text ? message.content.data
                type: 'text'
                stream: 'stdout'
            }
        else if msg_type == 'pyerr' or msg_type == 'error'
            stack = message.content.traceback
            stack = _.map stack, (item) -> item.trim()
            stack = stack.join('\n')
            return {
                data: stack
                type: 'text'
                stream: 'error'
            }

    destroy: ->
        console.log "sending shutdown"


        @kernelProcess.kill('SIGKILL')


    getGrammarForLanguage: (language) ->
        matchingGrammars = atom.grammars.getGrammars().filter (grammar) ->
            grammar != atom.grammars.nullGrammar and
