PIM-3319: Fix exceptions and specs + refactor some code