Several fixes to the image generation and caching code, again for the special error case