minor refactoring. Much more to be done.