Refactoring, bugfixes, improvements