Rewrite large parts of persistent dirstack handling