Hi Mazal,
I see the COLLECT statement only in the first loop in the code extract you shared so I will restrict myself to my observation regarding to that loop only. You are looping and collecting in the same table. You should use a separate table. Plus, I suggest that you read the F1 help available for the COLLECT statement and that should help you identify the issue.
Regards,
Akshat