refactoring and added comments.