line 42 starts a loop
line 43 declare imgs, throwing away the other imgs. It belongs after 37.
the loop on line 42 and the loop on line 38 should be merged into a single loop.
It seems you have gone array-happy making arrays, that you neither use nor need.
JΛ̊KE