Refactor code for the image creation