move logic from controller to model