Moved loop out of test function