Bugfixes and small cleanups