     # console.log 'deactivate markdown-preview-enhanced'
 
   toggle: ->
-    editor = atom.workspace.getActiveTextEditor()
+    editor = atom.workspace.getActivePaneItem()
     preview = @getPreviewForEditor(editor)
+
     if preview?.isOnDom()
       pane = atom.workspace.paneForItem(preview)
       pane.destroyItem(preview) # this will trigger preview.destroy()
-      delete @previewsMap[editor.getPath()]
+      @removePreviewFromMap preview
     else
       ## check if it is valid markdown file
       @startMDPreview(editor)

  getPreviewForEditor: (editor)->
    if @singlePreview
      return @getSinglePreview()
    else
      return @previewsMap[editor?.getPath?()]




  runCodeChunk: ()->
    editor = atom.workspace.getActivePaneItem()
    preview = @getPreviewForEditor(editor)
    if preview?.isOnDom()
      preview.runCodeChunk()
