Fix a bad unref and a memory leak