Optimization

Optimized the code, validated, and made the code easier to read