Changed pruning to happen before file write