improved code in validate