Finish database refactoring