Refactoring message handling