a bit of refactoring to allow for easier control of number of threads