Improved events handling (fixes issue #20)