Various refactorings around the new static/dynamic split.
Fixed some issues with threadId.