fixed memory leaks; added more unit testing