Fixing issue #291 (WIP)

this is wrong...it should add another event listener to beforeunload that calls sync.cleanupFn()