cleanups and bugfixes all over the place