modified to use tmp for output images