Refactored message encoding and decoding into sub-functions. This should
make the options easier to implement clearly.