Fixed memory leaks in algorithm.