Refactoring and comments.

Remove unused variables and debug statements.
Add docstring comments for clarity.