Bugfixes and cleanup.