Misc. fixes and optimizations.