Move logic out of controller