Misc cleanup and optimizations