Refactored code and commented relevant lines