Refactored to extract the controller from the logic.