Fixing scope issue for cron job code