Initial refactoring to fix problems created by moving to separate module.