added outer loop