Refactoring & bugfixes.