some refcounting fixes and speedups