moved logic from controller to model to dry it up