Added more comments to the refactored code.