Check keys instead of values

Fixes #563
Merge pull request #565 from he-li/#563

Check keys instead of values