Thread related fixes

/cc @daniel-marschner @schlubbi