fixed loop stack corruption in functions