merged two functions into one for loop