refactor

refactor with input validation