Event handling improvements. Fixes #161