Code

from Johan - fixes a memory leak