Code cleanups and bugfixes.