Improve event handling, make it more generic.