fixing mem leaks, there's quite a lot.