Created by: jirutka
This is updated and slightly modified patch from #7529 that fixes #6417 (closed).