Completed first version; added error handling, command line params; fix some bugs