Fixed memory leak after refactoring.