Refactoring & bugfixes