Moved all threading code into threads.c