Replace while loops with for loops, catch blank `img` folder error