fixed error handling & added a command line option