Implemented most of the controller.