Refactoring query_worker