move the validity check to its own function.