Fix memory leaks
Merge pull request #673 from stefanbeller/memleaks

Fix memory leaks