Fix rare bugs identified by fuzzing
Merge pull request #485 from thotypous/master

Fix rare bugs identified by fuzzing