Improve validate function