fixed memory freeing bug; more test code