Add event handling and cleanup functions