Added comments and refactored methods for clarity.