Bug fixes

Here is the PR that was dropped by the repo change.
I even added one new bugfix.