Fixed EOFError at the end of a received message.
Cleanup and minor fixes.