Re-factoring of the code for the scheduled task