Without looking at your code, the most common cause of problems in recursion is improperly scoped variables. You want all variables to be local. The only inputs should be parameters passed in. Generally those parameters should be by value unless you really, really know what you're doing and you're willing to do some brutal debugging if it doesn't work as you expect.
Regards. Al
"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov
Neither a despot, nor a doormat, be
Every app wants to be a database app when it grows up