Lots of changes to bring this up to Moodle 2.7 standards