Some changes to reduce memory leaks.