Memory cleanups and fix for `echo "foo" | sed 's/foo/bar/;H;q'`