moved event handling into module, minor refactoring