refactored code, added some input validation