[MONDRIAN-1998] Forward-port of the fixes for MONDRIAN-1998. Commits 707bf57955897ef4b2ea64dece184796d89c8f22 20a88f96e2486cd23bab8bca58c2dde186b285f2 29000ebd46be96b75e91724d80659459fca3659f 378a968651959f2ef1ec641ad1966b958ad7c3aa f8b96c28ad2b36149ea41155c50e1579f7be3ea8