Fix bug #1222
(cherry picked from commits 6ab4d6604991500b4508ee309aaea85b2b3a9f72, 2de58f31601f50bcb844b9acca07048768718d48)