move logic code from controller to model