Ported the threadlib (mostly)