fixed further issues with memory management