refactored some event code