Moved the flush of the task list to the beginning of the loop