Implement message flattener and expander.

Closes #181.