Clean up message parsing and add tests