Fixed memory leaks in image saving portion of the program