A few recent changes before I scrap most of this and start again

The main context loop is not going to handle dropping out of a context
and continuing mid way through the previous one. A recursing set of
functions should be better for this.