Various cleanups and bugfixes.