Optimization and cleanup