fixed some errors and tried to run it, but I'm reaching max recursion depth when calling rsolve. Probablly still an infinite loop (I hope.)