Restructured code in controller.php to use functions.