Refactoring, bugfixing