demote global variables into script variables where possible