Improve validation logic

Fixes #974