TODO: move this logic out of the controller.