Refactor the message logic