Further work on database refactor