refactored threading code.