Refactoring query worker