loop first

the best working version of the code