Optimizations and cleanup