Added documentation and did some refactoring.