Fixed a few reference counting bugs