properly handle IO errors