Better event handling, and some other stuff