Move the code for deleting intermediate files to its own predicate