First part of memory leak cleanups from Gilles