Refactoring. Separate function to do the actual query.