more fixes
more ad-hoc bugfixes, code will be cleaned up later