More database refactoring