Added some thread management