Merge pull request #2073 from tsuoranta/fix-memory-corruption