Fixed some memory leaks and added the tracking.