Added some code for event handling in user scripts