Reorganized to use addmessage for messages. Also minor cleanups of variable names, and better handling when there are no pg files under templates.
Changed paths: