Fixes to various non-obvious bugs

I played around with a static analysis tool and found some issues.
Hm, `toString()` returns a `std::string`. Maybe @ktrappe can comment?