refactoring message handling