added errorcheck for i/o