Refactor thread module internals