better event handling; refactoring