Moved code from controller to model
Moved code from controller to model