extended and fixed validation code