Some fixes and optimizations.