Slightly modified error handling code