 
 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)
+  if tabInfoMap[tabId]
+    updatePositionsAndWindowsForAllTabsInWindow(tabInfoMap[tabId].windowId)
   updatePositionsAndWindowsForAllTabsInWindow(attachedInfo.newWindowId)
 
 chrome.tabs.onMoved.addListener (tabId, moveInfo) ->
   updatePositionsAndWindowsForAllTabsInWindow(moveInfo.windowId)
 
 chrome.tabs.onRemoved.addListener (tabId) ->
-  openTabInfo = openTabs[tabId]
+  openTabInfo = tabInfoMap[tabId]
   updatePositionsAndWindowsForAllTabsInWindow(openTabInfo.windowId)
 
   # If we restore pages that content scripts can't run on, they'll ignore Vimium keystrokes when they

updatePositionsAndWindowsForAllTabsInWindow = (windowId) ->
  chrome.tabs.getAllInWindow(windowId, (tabs) ->
    for tab in tabs
      openTabInfo = openTabs[tab.id]
      if (openTabInfo)
        openTabInfo.positionIndex = tab.index
        openTabInfo.windowId = tab.windowId)

if shouldShowUpgradeMessage()
  sendRequestToAllTabs({ name: "showUpgradeNotification", version: currentVersion })

# Ensure that tabInfoMap is populated when Vimium is installed.
chrome.windows.getAll { populate: true }, (windows) ->
  for window in windows
    for tab in window.tabs
