Fix bug with memory leak