refactored messageHandler