Separate master and worker code into separate files.
Improve tests.