This introduces a thread-state object and moves various C globals