Fixed memory leak.

Code needs refactoring.