Refactor message encoding and decoding, make the encoder selectable at runtime