Some bugfixes and cleanup.