improve validation, clean up code