partial implementation of reference counting + def refactoring