I didn't have patience to read your script but a simple way to thing about it:
you have working script.
k, put it as one routine.
then do a "superscript" which calls for that script.

this way, you can keep the structure as simple as possible.

okey...
the superscript is then pretty simple.
the script is just:
do until $we_have_to_stop<>0
and in it you just count if it is divideable with 5...

I'm not giving you a script yet. just a way of thinking how you can keep it manageable in your or in my mind.
_________________________
!

download KiXnet