improved input validation