Minor rearrange code,
abstrat out the image generation part