move localstorage logic to window.close