added error handling and fixed CLI