added error checking in saving image