More bugfixes and cleanups