move loop definition up