move functions from controller to lib