add debugging, clean memory leaks