Various bugfixes and tweaks.