rewrite for loop