Refactoring a bunch of code in worker. No functional change.