refactor validate function