Improved the way event handlers are cleaned up.