Fixed some reference counting issues