Removed the second function and merged it in the for loop to increase performance.