PIM-2948: fix bugs and tests