slightly modified error handling