PIM-3312: fix specs, tests and clean code