Refactor the third learning stage into a separate function