refactored for loops into a single for loop
fixed an error from refactoring