Merged in the first pass code clean up using modules. This passes unit and ext-tests.