Bugfixes and cleanup