refactored some of the DB code to improve test performance