Code refactoring and add comments for clarity.
Code refactoring and add comments for clarity.