Fix waiting on the frontend/backend

I'm not sure I got all the cases, but this is what the general
direction should look like.  Some of the code is still very ugly,
though.