Refactor thread control.