fix variable scopes

the script local variables are in the wrong scope.

makes the source a little bit easier to understand.