Clean up blocks and add sleep value.

Blocks are more idiomatic.  I wish it wasn't so nested though. Hmm.
Split buffer update and end of message detection into separate methods.

Break things out into separate methods to reduce nesting.  Also,
keep message buffer variable in one place (in the loop context).