restructered code
Optimized code