This code doesn't belong to the controller and should be refactored.