consolidate some code and fix import-names issue