             catch error
                 return
 
+    closeUnpinnedTabs: ->
+        activePane = document.querySelector '.pane.active'
+        tabbar = activePane.querySelector '.tab-bar'
 
-    # Method to pin the active tab.
+        activePane = atom.workspace.getActivePane()
+        tabs = tabbar.querySelectorAll '.tab'
+        for i in [(tabs.length - 1)..0]
+            if !tabs[i].classList.contains('pinned')
+                activePane.itemAtIndex i
+                activePane.destroyItem activePane.itemAtIndex(i)
+
+
+	# Pin tabs
     pinActive: ->
         @pin document.querySelector('.tab-bar .tab.active')
 
-    # Method to pin the selected (via contextmenu) tab.
     pinSelected: ->
         @pin atom.contextMenu.activeElement
 
-    # Method that pins/unpins a tab given its element.
     pin: (e) ->
-        # Get information about the tab
-        try
-            tab = @getTabInformation e
-        catch error
-            return
+        return unless tab = @getTabInformation e
 
-        # Calculate the new index for this tab based
-        # on the amount of pinned tabs within this pane.
         if tab.isPinned
-            # If the element has the element 'pinned', it
-            # is currently being unpinned. So the new index
-            # is one off when look at the amount of pinned
-            # tabs, because it actually includes the tab
-            # that is being unpinned.
-            tab.pinIndex -= 1
-
-            # Removed one pinned tab from the state key for this pane.
             @PinnedTabsState.data[tab.paneIndex] -= 1
+            tab.pane.moveItem(tab.item, tab.unpinIndex)
         else
-            # Initialize the state kye for this pane if needed.
+            # Initialize the state key for this pane if needed.
             @PinnedTabsState.data[tab.paneIndex] = 0 if @PinnedTabsState.data[tab.paneIndex] == undefined
 
-            # Add one pinned tab from the state key for this pane.
             @PinnedTabsState.data[tab.paneIndex] += 1
+            tab.pane.moveItem(tab.item, tab.pinIndex)
 
-        # Move the tab to its new index
-        tab.pane.moveItem(tab.item, tab.pinIndex)
-
-        # Finally, toggle the 'pinned' class on the tab after a
-        # timout of 1 millisecond. This will ensure the animation
-        # of pinning the tab will run.
-        callback = -> e.classList.toggle 'pinned'
-        setTimeout callback, 1
+        setTimeout (->
+            e.classList.toggle 'pinned'
+        ), 1
 
 
-    # Get information about a tab
+    # Utility
     getTabInformation: (e) ->
-        # Get related nodes
-        tabbar = e.parentNode
-        paneNode = tabbar.parentNode
-        axis = paneNode.parentNode
+        tabbarNode = e.parentNode
+        paneNode = tabbarNode.parentNode
+        axisNode = paneNode.parentNode
 
-        # Get the index values of relevant elements
-        curIndex = Array.prototype.indexOf.call(tabbar.children, e)
-        paneIndex = Array.prototype.indexOf.call(axis.children, paneNode)
+        tabIndex = Array.prototype.indexOf.call(tabbarNode.children, e)
+        paneIndex = Array.prototype.indexOf.call(axisNode.children, paneNode)
         pinIndex = paneNode.querySelectorAll('.pinned').length
 
-        # Get the related pane & texteditor
         pane = atom.workspace.getPanes()[paneIndex / 2]
-        item = pane.itemAtIndex curIndex
+        item = pane.itemAtIndex(tabIndex)
 
         return {
-            curIndex: curIndex,
+            tabIndex: tabIndex,
+            paneIndex: paneIndex,
+
             pinIndex: pinIndex,
-            paneIndex: paneIndex,
+            unpinIndex: pinIndex - 1,
 
             itemNode: undefined,
             paneNode: undefined,

            else
                body.classList.add 'pinned-tabs-enable-modified'

    # Observer panes
    observers: ->
        atom.workspace.onDidAddPaneItem (event) =>
            setTimeout (=>
                # Get information about the tab

                try
                    # Find the pane and tab-bar DOM objects for
                    # this pane.
                    pane = panes.children[parseInt(key, 10)]
                    tabbar = pane.querySelector '.tab-bar'

                    # Pin the first N tabs, since pinned tabs are
