Optimization and cleaning code