fix reference counting errors