Fixed a refactoring bug leading to a memory leak