thread fix

clean PR with fixes to #6061 as suggested by @deguio