updated threading code