Added stronger type-checking for localStorage.