code refactor.
refactoring code and adding comments.