diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/RRuntimeASTAccessImpl.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/RRuntimeASTAccessImpl.java
index 8eae3e3e08592631c4b50ecdaeb34154b6454a38..8a329dc6d3056c563da5b7bd62584daa21dcc5d9 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/RRuntimeASTAccessImpl.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/RRuntimeASTAccessImpl.java
@@ -36,7 +36,6 @@ import com.oracle.truffle.api.RootCallTarget;
 import com.oracle.truffle.api.TruffleLanguage;
 import com.oracle.truffle.api.frame.Frame;
 import com.oracle.truffle.api.frame.FrameInstance.FrameAccess;
-import com.oracle.truffle.api.frame.MaterializedFrame;
 import com.oracle.truffle.api.nodes.Node;
 import com.oracle.truffle.api.nodes.RootNode;
 import com.oracle.truffle.api.object.DynamicObject;
@@ -90,7 +89,6 @@ import com.oracle.truffle.r.runtime.data.RStringVector;
 import com.oracle.truffle.r.runtime.data.RSymbol;
 import com.oracle.truffle.r.runtime.data.model.RAbstractContainer;
 import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector;
-import com.oracle.truffle.r.runtime.env.REnvironment;
 import com.oracle.truffle.r.runtime.nodes.InternalRSyntaxNodeChildren;
 import com.oracle.truffle.r.runtime.nodes.RBaseNode;
 import com.oracle.truffle.r.runtime.nodes.RCodeBuilder;
@@ -733,25 +731,4 @@ class RRuntimeASTAccessImpl implements RRuntimeASTAccess {
     public RAbstractStringVector getClassHierarchy(RAttributable value) {
         return ClassHierarchyNode.getClassHierarchy(value);
     }
-
-    @Override
-    public String getQualifiedFunctionName(RootNode n) {
-
-        if (n instanceof FunctionDefinitionNode && n.getName() != null) {
-            FunctionDefinitionNode fdn = (FunctionDefinitionNode) n;
-            StringBuilder qname = new StringBuilder(n.getName());
-            MaterializedFrame frame = (MaterializedFrame) Utils.getCallerFrame(Utils.getActualCurrentFrame(), FrameAccess.MATERIALIZE);
-            REnvironment env = REnvironment.frameToEnvironment(frame);
-
-            while (env != null && env.getPrintName() != null) {
-                qname.insert(0, '.');
-                qname.insert(0, env.getPrintName());
-                env = env.getParent();
-            }
-            return qname.toString();
-
-        }
-        return null;
-
-    }
 }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
index d6025670c083a490dc4e4dd5285f52f66b211f22..c84b208bba1944a22ad28500a845dcf6ca347aa1 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
@@ -30,7 +30,6 @@ import javax.xml.bind.DatatypeConverter;
 
 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
 import com.oracle.truffle.api.interop.TruffleObject;
-import com.oracle.truffle.api.nodes.Node;
 import com.oracle.truffle.api.nodes.RootNode;
 import com.oracle.truffle.api.object.DynamicObject;
 import com.oracle.truffle.api.source.Source;
@@ -349,7 +348,7 @@ public class RDeparse {
                 assert printHexBinary.length() > 10;
 
                 // just use the first 10 hex digits to have a nicer file name
-                if (qualifiedFunctionName != null) {
+                if (qualifiedFunctionName != null && !qualifiedFunctionName.isEmpty() && !qualifiedFunctionName.equals("<no source>")) {
                     path = tmpDir.resolve(qualifiedFunctionName + "-" + printHexBinary.substring(0, 10) + ".r");
                 } else {
                     path = tmpDir.resolve(printHexBinary.substring(0, 10) + ".r");
@@ -368,10 +367,9 @@ public class RDeparse {
         public void fixupSources() {
             if (FastROptions.EmitTmpSource.getBooleanValue()) {
                 try {
-                    RootNode rn = getRootNode();
-                    String qualifiedFunctionName = RContext.getRRuntimeASTAccess().getQualifiedFunctionName(rn);
-
-                    Path path = emitToFile(qualifiedFunctionName);
+                    RootNode rootNode = getRootNode();
+                    String name = rootNode != null ? rootNode.getName() : null;
+                    Path path = emitToFile(name);
                     Source source = RSource.fromFile(path.toFile());
                     for (SourceSectionElement s : sources) {
                         s.element.setSourceSection(source.createSection(s.start, s.length));
@@ -390,18 +388,12 @@ public class RDeparse {
         }
 
         private RootNode getRootNode() {
-            RootNode rn = null;
-            for (SourceSectionElement s : sources) {
-                if (s.element instanceof Node) {
-                    Node n = (Node) s.element;
-                    if (rn == null) {
-                        rn = n.getRootNode();
-                    } else {
-                        assert rn == n.getRootNode();
-                    }
-                }
+            // the last element in the list is the top-most one
+            RSyntaxElement n = sources.get(sources.size() - 1).element;
+            if (n instanceof RootNode) {
+                return (RootNode) n;
             }
-            return rn;
+            return null;
         }
 
         @SuppressWarnings("try")
@@ -1072,7 +1064,20 @@ public class RDeparse {
     public static void ensureSourceSection(RSyntaxNode node) {
         SourceSection ss = node.getLazySourceSection();
         if (ss == RSyntaxNode.LAZY_DEPARSE) {
-            new DeparseVisitor(true, RDeparse.MAX_Cutoff, false, -1, 0).append(node).fixupSources();
+            RSyntaxElement nodeToFixup = node;
+            if (FastROptions.EmitTmpSource.getBooleanValue()) {
+                RootNode rootNode = node.asNode().getRootNode();
+                if (RContext.getRRuntimeASTAccess().isFunctionDefinitionNode(rootNode)) {
+                    nodeToFixup = (RSyntaxElement) rootNode;
+                }
+            }
+            // try to generate the source from the root node and hopefully it includes this node
+            new DeparseVisitor(true, RDeparse.MAX_Cutoff, false, -1, 0).append(nodeToFixup).fixupSources();
+
+            // if not, we have to deparse the node in isolation
+            if (node.getLazySourceSection() == RSyntaxNode.LAZY_DEPARSE) {
+                new DeparseVisitor(true, RDeparse.MAX_Cutoff, false, -1, 0).append(node).fixupSources();
+            }
             assert node.getLazySourceSection() != RSyntaxNode.LAZY_DEPARSE;
         }
     }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntimeASTAccess.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntimeASTAccess.java
index 13f1f0abb1fb8b8aa549f87f087a5ed7afa9e2b2..15c84e15e28fe07d215ca5a9e60a6ff9f1b8d959 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntimeASTAccess.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntimeASTAccess.java
@@ -204,11 +204,4 @@ public interface RRuntimeASTAccess {
     void checkDebugRequest(RFunction func);
 
     RAbstractStringVector getClassHierarchy(RAttributable value);
-
-    /**
-     * Creates a qualified name of an R function.
-     *
-     * @param n The root node of the function (i.e. the function definition node).
-     */
-    String getQualifiedFunctionName(RootNode n);
 }