Optimization and code cleanup