minor refactoring and added comments.