Refactored scripts to explicitly define local variables where appropriate.