Fixed numerous pretty serious memory leaks.