refactoring and a bugfix for import code