Moved logic from controller to model.