diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php
index aa88cb0c755..576324f2a5e 100644
--- a/mod/forum/discuss.php
+++ b/mod/forum/discuss.php
@@ -64,7 +64,7 @@
             error('Forum not visible', $return);
         }
 
-        if (!forum_move_attachments($discussion, $forumto)) {
+        if (!forum_move_attachments($discussion, $forumto->id)) {
             notify("Errors occurred while moving attachment directories - check your file permissions");
         }
         set_field('forum_discussions', 'forum', $forumto->id, 'id', $discussion->id);
@@ -76,7 +76,7 @@
         // Delete the RSS files for the 2 forums because we want to force
         // the regeneration of the feeds since the discussions have been
         // moved.
-        if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($sourceforum)) {
+        if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($forumto)) {
             error('Could not purge the cached RSS feeds for the source and/or'.
                    'destination forum(s) - check your file permissionsforums', $return);
         }
diff --git a/mod/forum/lib.php b/mod/forum/lib.php
index f6dae950746..829ad58ffac 100644
--- a/mod/forum/lib.php
+++ b/mod/forum/lib.php
@@ -3185,20 +3185,22 @@ function forum_go_back_to($default) {
 function forum_file_area_name($post) {
     global $CFG;
 
-    if (!isset($post->forum)) {
-        debugging('missing forum', DEBUG_DEVELOPER);
+    if (!isset($post->forum) or !isset($post->course)) {
+        debugging('missing forum or course', DEBUG_DEVELOPER);
         if (!$discussion = get_record('forum_discussions', 'id', $post->discussion)) {
             return false;
         }
         if (!$forum = get_record('forum', 'id', $discussion->forum)) {
             return false;
         }
-        $forumid = $forum->id;
+        $forumid  = $forum->id;
+        $courseid = $forum->course; 
     } else {
-    	$forumid = $post->forum;
+    	$forumid  = $post->forum;
+        $courseid = $post->course; 
     }
 
-    return "$post->course/$CFG->moddata/forum/$forumid/$post->id";
+    return "$courseid/$CFG->moddata/forum/$forumid/$post->id";
 }
 
 /**
@@ -3364,10 +3366,15 @@ function forum_add_new_post($post,&$message) {
 
     global $USER, $CFG;
 
-    $post->created = $post->modified = time();
-    $post->mailed = "0";
-    $post->userid = $USER->id;
+    $discussion = get_record('forum_discussions', 'id', $post->discussion);
+    $forum      = get_record('forum', 'id', $discussion->forum);
+
+    $post->created    = $post->modified = time();
+    $post->mailed     = "0";
+    $post->userid     = $USER->id;
     $post->attachment = "";
+    $post->forum      = $forum->id;     // speedup
+    $post->course     = $forum->course; // speedup
 
     if (! $post->id = insert_record("forum_posts", $post)) {
         return false;
@@ -3431,13 +3438,15 @@ function forum_update_post($post,&$message) {
  */
 function forum_add_discussion($discussion,&$message) {
 
-    GLOBAL $USER, $CFG;
+    global $USER, $CFG;
 
     $timenow = time();
 
     // The first post is stored as a real post, and linked
     // to from the discuss entry.
 
+    $forum = get_record('forum', 'id', $discussion->forum);
+
     $post = new object();
     $post->discussion  = 0;
     $post->parent      = 0;
@@ -3448,8 +3457,8 @@ function forum_add_discussion($discussion,&$message) {
     $post->subject     = $discussion->name;
     $post->message     = $discussion->intro;
     $post->attachment  = "";
-    $post->forum       = $discussion->forum;
-    $post->course      = $discussion->course;
+    $post->forum       = $forum->id;     // speedup
+    $post->course      = $forum->course; // speedup
     $post->format      = $discussion->format;
     $post->mailnow     = $discussion->mailnow;