Various small fixes and one big fix in the scripting SM event handling code