Cleanups and bugfixes