Refactored DB code.