updated to use built-in script context save/restore