added if statement in image loop