Added more validation to avoid errors