Changed variable scopes and exported functions

Moved all variables from $global to $script to not pollute the $Global
scope. Added exporting of functions so helper functions are not exposed.
Moved back to Global scope

More troubleshooting than I care to do at midnight!