added slightly better error handling