bugfixes and cleanup.