Refactoring, bugfixes