move some code from route to model