Make main loop run in its own thread.

This is probably awful, but it's all I could figure out.