Refactored setting the query in separate function