Fix bug causing messages to have extra characters or failing to encode properly.