Refactoring to decouple UI and logic.