various small bugfixes and refactors