Cleaned up thread code.