diff --git a/breadcrumbs.py b/breadcrumbs.py
index 08f8ece..f7fc4b7 100644
--- a/breadcrumbs.py
+++ b/breadcrumbs.py
@@ -1,15 +1,15 @@
 from trac.core import Component, implements, TracError
 from trac.config import Option, IntOption, ListOption
+from trac.resource import Resource, ResourceSystem
 from trac.web import IRequestFilter
-from trac.wiki import parse_args
 from trac.web.chrome import ITemplateProvider, add_stylesheet
-from pkg_resources import resource_filename
 from trac.web.api import ITemplateStreamFilter
 from genshi.builder import tag
 from genshi.filters.transform import Transformer
 import re, cPickle
 from trac.env import IEnvironmentSetupParticipant
 
+
 class BreadCrumbsSystem(Component):
     implements(IRequestFilter, ITemplateProvider, ITemplateStreamFilter, IEnvironmentSetupParticipant)
     
@@ -19,44 +19,45 @@ class BreadCrumbsSystem(Component):
     
     max_crumbs = IntOption('breadcrumbs', 'max_crumbs', 6, 
         doc="""Indicates the maximum number of breadcrumbs to store per user.""")
+    supported_realms = ListOption('breadcrumbs', 'realms', 'wiki, ticket, milestone, pastebin, report, changeset, source')
     
     supported_paths = ListOption('breadcrumbs', 'paths', '/wiki*,/ticket*,/milestone*',
         doc='List of URL paths to allow breadcrumb tracking. Globs are supported.')
+
+    label = Option('breadcrumbs','label','Breadcrumbs:',
+                   doc="""Text label to show before breadcrumb list""")
     
     compiled_ignore_pattern = None
     
     ## IEnvironmentSetupParticipant
     def environment_created(self):
-        self._upgrade_db(self.env.get_db_cnx())
+        self._upgrade_db()
 
-    def environment_needs_upgrade(self, db):
+    def environment_needs_upgrade(self, db=None):
+        if not db:
+            db = self.env.get_read_db()
         cursor = db.cursor()
-
         try:
             cursor.execute(
-                "SELECT count(*) FROM session_attribute WHERE name = %s", 
+                "SELECT count(*) FROM session_attribute WHERE name = %s",
                 ("breadcrumbs list",)
             )
             result = cursor.fetchone()
             if int(result[0]):
                 return True
-                
             return False
         except:
-            db.rollback()
             return True
 
-    def upgrade_environment(self, db):
+    def upgrade_environment(self, db=None):
         self._upgrade_db(db)
 
-    def _upgrade_db(self, db):
+    def _upgrade_db(self, db=None):
         try:
-            from trac.db import DatabaseManager
-            db_backend, _ = DatabaseManager(self.env)._get_connector()            
-
-            cursor = db.cursor()
-            cursor.execute("DELETE FROM session_attribute WHERE name = %s", ("breadcrumbs list",))
-
+            @self.env.with_transaction(db)
+            def do_upgrade(db):
+                cursor = db.cursor()
+                cursor.execute("DELETE FROM session_attribute WHERE name = %s", ("breadcrumbs list",))
         except Exception, e:
             db.rollback()
             self.log.error(e, exc_info=True)
@@ -70,56 +71,65 @@ class BreadCrumbsSystem(Component):
         
     def _get_crumbs(self, sess):
         crumbs = []
-        if 'breadcrumbs_list' in sess:
-            raw = sess['breadcrumbs_list']
+        if 'breadcrumbs' in sess:
+            raw = sess['breadcrumbs']
             try:
-                crumbs = cPickle.loads(raw.encode('ascii', 'ignore'))
+                dicts = cPickle.loads(raw.encode('ascii', 'ignore'))
+                for d in dicts:
+                    crumbs.append(self._dict_to_resource(d))
             except:
-                del sess['breadcrumbs_list']
-        
+                del sess['breadcrumbs']
         return crumbs
+    
+    def _dict_to_resource(self, rdict):
+        resource = Resource()
+        for k in resource.__slots__:
+            setattr(resource, k, rdict[k])
+        if rdict.get('parent'):
+            resource.parent = self._dict_to_resource(rdict['parent'])
+        return resource
+    
+    def _resource_to_dict(self, resource):
+        rdict = dict([(k, getattr(resource, k)) for k in resource.__slots__])
+        if resource.parent:
+            rdict.update(parent=self._resource_to_dict(resource.parent))
+        return rdict
         
     def post_process_request(self, req, template, data, content_type):
+        def _break():
+            return template, data, content_type
+        if req.authname == 'anonymous':
+            return _break()
+        resource = None
+        try:
+            resource = data['context'].resource
+        except:
+            self.log.debug('No context/resource found, path=%s', req.path_info)
+            return _break()
+        if not resource or resource.realm not in self.supported_realms:
+            return _break()
         if self.compiled_ignore_pattern is None and self.ignore_pattern:
             self.compiled_ignore_pattern = re.compile(self.ignore_pattern)
-        
-        path = req.path_info
+        if (self.compiled_ignore_pattern and
+                            self.compiled_ignore_pattern.match(resource.id)):
+            return _break()
+        mgr = ResourceSystem(self.env).get_resource_manager(resource.realm)
         try:
-            if path.count('/') >= 2:
-                _, realm, resource = path.split('/', 2)
-            
-                supported = False
-            
-                for pattern in self.supported_paths:
-                    if re.match(pattern, path):
-                        supported = True
-                        break
-                
-                if not supported or (self.compiled_ignore_pattern and
-                            self.compiled_ignore_pattern.match(resource)):
-                    return template, data, content_type
-                
-                if '&' in resource:
-                    resource = resource[0:resource.index('&')]
-                
-                sess = req.session
-                crumbs = self._get_crumbs(sess)
-                
-                current = '/'.join( (realm, resource) )
-                if current in crumbs:
-                    crumbs.remove(current)
-                    crumbs.insert(0, current)
-                else:
-                    crumbs.insert(0, current)
-                    crumbs = crumbs[0:self.max_crumbs]
-                    
-                sess['breadcrumbs_list'] = cPickle.dumps(crumbs)
+            # We might have an unfinalized resource (/newticket will have a ticket
+            # resource without id in the context for instance)
+            assert mgr.resource_exists(resource)
         except:
-            self.log.exception("Breadcrumb failed :(")
-        
-        
+            return _break()
+        crumbs = self._get_crumbs(req.session)
+        if resource in crumbs:
+            crumbs.remove(resource)
+            crumbs.insert(0, resource)
+        else:
+            crumbs.insert(0, resource)
+            crumbs = crumbs[:self.max_crumbs]
+        req.session['breadcrumbs'] = cPickle.dumps([self._resource_to_dict(c) for c in crumbs])
         return template, data, content_type
-    
+        
     ## ITemplateProvider
     
     def get_htdocs_dirs(self):
@@ -135,31 +145,28 @@ class BreadCrumbsSystem(Component):
         crumbs = self._get_crumbs(req.session)
         if not crumbs:
             return stream
-            
         add_stylesheet(req, 'breadcrumbs/css/breadcrumbs.css')
         li = []
-
-        href = req.href(req.base_path)
-        
-        for crumb in crumbs:
-            realm, resource = crumb.split('/', 1)
-            name = resource.replace('_', ' ')
-
-            if realm == "ticket":
-                name = "#" + resource
-            elif realm != "wiki":
-                name = "%s:%s" % (realm, name)
-
-            link = req.href(realm, resource)
-            
+        for r in crumbs:
+            mgr = ResourceSystem(self.env).get_resource_manager(r.realm)
+            try:
+                name = mgr.get_resource_description(r, format='compact')
+                title = mgr.get_resource_description(r, format='summary')
+            except:
+                self.log.exception('Failed to lookup resource %s', r)
+                continue
+            if hasattr(mgr, 'get_resource_url'):
+                link = mgr.get_resource_url(r, req.href)
+            else:
+                # try a guess
+                link = req.href(r.realm, r.id)
             li.append(
                 tag.li(
-                    tag.a(title=name, href=link,
-                    )(name)
+                    tag.a(name, title=title, href=link)
                 )
             )
             
-        insert = tag.ul(class_="nav", id="breadcrumbs")(tag.lh("Breadcrumbs:"), li)
+        insert = tag.ul(class_="nav", id="breadcrumbs")(tag.lh(self.label), li)
 
         return stream | Transformer('//div[@id="metanav"]/ul').after(insert)
         
