completed code for generation of images and minor fixes