added image rewriting, close streams