Factor persistence code into a separate namespace, add test