better thread management, fix thread lock