Refactored code to make things easier with validation error output. Added tests.