Refactored code +added more comments