Refactored clearMessages method to a more general clearEvents