Added some error checking (dies if it doesn't find what it needs at various points)
Added error checking and more sophisticated command line parsing with getopt