Replaced some for each statements with for statements.