before repairig loops working version