more refactoring and bugfixes