rewrote to cimg loops - much much faster