Fixes writing of encoded keys to storage, and encoding empty key arrays.