Full working implementation of the controller.