improved error handling code a fair amount