rewrite the image memory management