Fixed memory bugs.
Merge pull request #5 from tomco/master

Memory bugs