Fixes to threaded code