in reply to Help with my rookie logic

If I download your code and try to run it, I get a compile error:
Unterminated <> operator

The following:

... and $orig_cnt=1)

is an assignment. It should probably be a numeric check (==):

... and $orig_cnt==1)

I also see that in two other places in your code.

Other advice: