refactored the validation logic and unit test