fixed small bug in image writing procedure