replaced redundant statements with for loops