Refactored the importing code.

A mode robust fix to #222.