refactor threading into separate module.