more robust localstorage code