Plugin shutdown routine; minor cleanups

 - Added a new plugin method that gets called just before shutdown

 - Made a few global internal variables static since they're not useful
   to external code

 - Minor comment cleanups etc.