improved error handling; added exit(1)