Refactoring and code improvement to implement validation