moving worker function out to make code more readable