Add comments and refactor for clarity