Minor bugfixes & refactoring.