Some cleanups and minor optimizations