Added more comments and refactoring.