Refactored and added comments
Merged