More cleanup and bugfixes