Fixing https://tracker.moodle.org/browse/CONTRIB-4322, thanks to Elizabeth Dalton
Fixing CONTRIB-3204