More changes for improved event handling. Code is now a lot robuster.