improved and fixed localStorage handling