Working threads code. Need to do more testing.