added some missing error handling and more detailed error messages