refactor and encapsulate the DB thing