Prevent exceptions when an account is deleted

I was having some trouble with on-device tests failing after deleting an account. I think there was a race where an account is deleted, but hangs around in the Memory class and therefore causes an exception which fails the test.

I'm not sure if passing around the Preferences object is the best solution to this, but it works.

Also removed a call to map with a null key, which prevents use of e.g. ConcurrentHashMap which doesn't handle null keys.