A lot of code which now works fine, it seems

But lacks documentation and tests!
Code fixes...