added subsets and fixed the memory leak