fixed incorrect encoding of the last character in a message
added debug output