Fixes some memory leaks
Merge pull request #564 from louisdx/memoryleaks

Fixes some memory leaks