Fixed bug in event detaching