Refactored event code