Cleanups and minor optimizations