Refactorings, bugfixes