Refactored message handling.