Minor refactoring and added comments.