Refactor finalize stage into Task class