Do not use exit in main loop

This makes it possible to cleanly source the script
Also, tighten up whitespace in main loop
Localizing unneeded globals
Fixed filename and version command line option logic