Improve validation code