Moved code out of controller into model