Tidied up class handling following #844

Rearranged the way we set classes, to avoid duplicating regexes between `setClasses.js` and `addTest.js`

Fixes #844 and #808