using forEach to iterate event elements
Fix bugs and refactoring