Added more validation to the code