Major refactoring to avoid import loop.