Port of the fix for MONDRIAN-1605 on master, commits 456cc85c71dddc919c5bb719f98843cbb3d11a35 and e3771dc03fbbc24a2106569e960c546cd22b2fae.