Fixed memory leaks, added doc.