Fixed some bugs when upgrading from Moodle 1.9