Device correctness

This branch isn't ready to merge yet, but I'm creating this pull request for discussion.