attempt2

some refactoring to cleanup.