Improve generated code.
Move functions around a bit.
Don't detach threads.

If we detach threads, we can't wait for them to finish. If we
can't do that, we can't free memory used in the thread pool.
IOW, we have to wait for the threads to finish.