more refactoring and tests done.