reapplying fix to #499, rolled back by #530

Nothing new. Same as PR #499.