Adding error checking to read/write image