First attempt to fixing the memory leak