Fixed memory leaks.

closes #148