Fixed the bug in ScriptEngine state manager. Unfortunately it didn't maintain the references correctly. This not an ideal fix at this moment as it requires copying the list of references over and over, but if we move to refactor the ScriptEngine we will address it anyway - this is out of scope here.