Catch STM exceptions in receive
Move logging functions to new module
Tidy up exception handling a bit