bugfixes and cleanup