   else
     tabQueue[openTabInfo.windowId] = [openTabInfo]
 
-  delete openTabs[tabId]
+  # keep the reference around for a while to wait for the last messages from the closed tab (e.g. for updating
+  # scroll position)
+  setTimeout (-> delete tabInfoMap[tabId]), 1000
   delete framesForTab[tabId]
 
 chrome.tabs.onActiveChanged.addListener (tabId, selectInfo) -> updateActiveState(tabId)


chrome.tabs.onAttached.addListener (tabId, attachedInfo) ->
  # We should update all the tabs in the old window and the new window.
  if openTabs[tabId]
    updatePositionsAndWindowsForAllTabsInWindow(openTabs[tabId].windowId)
  updatePositionsAndWindowsForAllTabsInWindow(attachedInfo.newWindowId)

chrome.tabs.onMoved.addListener (tabId, moveInfo) ->
  updatePositionsAndWindowsForAllTabsInWindow(moveInfo.windowId)

chrome.tabs.onRemoved.addListener (tabId) ->
  openTabInfo = openTabs[tabId]
  updatePositionsAndWindowsForAllTabsInWindow(openTabInfo.windowId)

  # If we restore pages that content scripts can't run on, they'll ignore Vimium keystrokes when they

      chrome.tabs.update(toSelect.id, { selected: true })))

updateOpenTabs = (tab) ->
  tabInfoMap[tab.id] = { url: tab.url, positionIndex: tab.index, windowId: tab.windowId }
  # Frames are recreated on refresh
  delete framesForTab[tab.id]

