fixed cleanup to properly set pointers to NULL, and added unit test (passing)