added logic to save user state in case the browser (or the app window) crashes