Fixed optimizations and cleanup