Fixes #533 (see comments) and #692

Note: this branch is based on the feature/auth branch, so it also includes its commits.