Fix main loop

The main loop was using a lot of cpus waiting, now we just block untill
the queue has something in it.