Updated threading code