fixed memory leaks.  Also added debugging code.