Some refactoring and adding comments.