Refactored and commented
Refactored and commented