Fixed major bug in ref count on databases. Added debug ref count output.