fixed the bug for reference counting