Better setmodel logic to fix issue #3644.