Here's my current version if you want to try it

Added some logging stuff and a few other minor things.  It's passing tests on my machine though so maybe it'll resolve your current issues?