Various fixes and optimizations.