General bugfixes and optimizations.