This seems like logic that should be in the model. There's a lot of logic here. Why move it into the controller