move some logic out of the controllers and into a seperate file