refactor the DB related code