Started refactoring db code