refactor message logic