Misc fixes and optimizations.