Refactored UI code with nested listener classes