added check for undefined and bestValid