Refactoring the alerts function