Corrected for running as a scheduled task