refactoring, folding in share-count code