Refactoring back into master

A bunch of refactoring to make more code common between different import types, bringing it back to master.