Documentation added.
Refactored code