added a check to ensure valid result