Finished adding comments, minor refactorings and documentation.