Detect IO errors and repport to caller

Based on error handling added to pratsam amis code fork