Reduce memory usage, fix a few potential crashes.