Refactoring validation code