Fix for memory leak, thread unsafe ref count.