Moved the function out of the loop.