added more error checking

fails more gracefully on io errors