Bugfix 40 memory leaks

Patched as discussed.