Attempted to fix memory leaks. Verification needed.