updated to eliminate more memory leaks