Random cleanup and bugfixes