cleaned up debug code, and modified to close figures programatically