Some bugfixes, cleanups and crutches.