Refactored code and added code documentation.