Refactored to fix alert issue