Refactoring and added comments.