Modularize message parsing, fix some bugs