Wrapped the loop in a function.