Some cleanup and minor optimizations.