combined two functions into one for loop