Bug fixes

Numerous small bugfixes, code is stable again!