Refactored code to make events perform better