Added threading. Some other cleanup.