Changed handler cleanup to also occur on tab close, since the page
unload event can occur some time after the tab is closed. The delayed
unload causes an error when removing the handler, causing it to not be
unloaded properly. Also now removes the all the handlers for the
unloading document prior to cleaning them up to ensure that they are
never left behind.