added checking to make sure result is valid