Added check for required programs at start of script. Fixed some bugs that I had introduced.