Thank you for your comment pointing out some bugs and suggestions for improvement!
I fixed the defective savepoints and a couple of other things.