Port Max's crash fix to my 3.6 fork
Merge pull request #694 from shifu/master

Port Max's crash fix to my 3.6 fork