working on loop definition algorithm