Fixed some bugs identified by Valgrind
Merge pull request #272 from peastman/master

Fixed some bugs identified by Valgrind