Refactoring and added some comments.