Refactoring database access functions