Refactoring request_db function