Bugfixes and cleanups