Wip replace stm

First commit was done whilst pairing with @thsutton, uncovered a concurrency issue involving a blocking receive whilst trying to simultaneously send. This was explained to @afcowie.

Latter two commits fix this issue and require further review and testing, by me and others. They were written quite quickly on a train.