Refactoring query handling in the database repo.