Refactor: pull DB side effects out into separate actions, bug fixes