Added documentation and refractored code.