Sorted out the infinite loop error plaguing the outer loop in the Function Method