Refcounting hooks

fixes #5
Merge branch 'refcounting'