write loops with muliple indices in the right order