refactoring to use event listeners