reorganizing the message-handling code a bit