Bugfixes and small refactors.