Misc optimizations and cleanups