clean up the whole 'finalize' and 'finalize_all' code