Minor refactoring
Refactoring and tests

More validation, tests, and refactoring