see #8671 - some code refactoring done in an unsuccessful attempt to reproduce error, but worth keeping