Memorymanagement

Fixes for #44 and #45.