Refactor processMessage()