Fix threading bugs, more decomposition

Removed the local array copies from the thread loop, as this meant
chunks wouldn't react properly (also wasn't copying correctly either).
Moved a bit more stuff out of main and switched to pthread_join rather
than semaphores (seems like the implementation is bugged on windows).