code validation rewritten