Refactored thread management