Work towards #2220
The code is more verbose and less efficient now, but should be reusable in add_import context as well