Added more refined error handling