Rewrite quiz logic to be more robust

Fixes #470