diff --git a/app/controllers/refs_controller.rb b/app/controllers/refs_controller.rb
index 188feb73ec1a053f87456092f2d0648576562526..69756bb8ecd2d127d658a0479177cc8679659be4 100644
--- a/app/controllers/refs_controller.rb
+++ b/app/controllers/refs_controller.rb
@@ -1,24 +1,22 @@
 class RefsController < ProjectResourceController
+  include ExtractsPath
 
   # Authorize
   before_filter :authorize_read_project!
   before_filter :authorize_code_access!
   before_filter :require_non_empty_project
 
-  before_filter :ref
-  before_filter :define_tree_vars, only: [:blob, :logs_tree]
-
   def switch
     respond_to do |format|
       format.html do
         new_path = if params[:destination] == "tree"
-                     project_tree_path(@project, (@ref + "/" + params[:path]))
+                     project_tree_path(@project, (@id))
                    elsif params[:destination] == "blob"
-                     project_blob_path(@project, (@ref + "/" + params[:path]))
+                     project_blob_path(@project, (@id))
                    elsif params[:destination] == "graph"
-                     project_graph_path(@project, @ref)
+                     project_graph_path(@project, @id)
                    else
-                     project_commits_path(@project, @ref)
+                     project_commits_path(@project, @id)
                    end
 
         redirect_to new_path
@@ -42,27 +40,4 @@ class RefsController < ProjectResourceController
       }
     end
   end
-
-  protected
-
-  def define_tree_vars
-    params[:path] = nil if params[:path].blank?
-
-    @repo = project.repository
-    @commit = @repo.commit(@ref)
-    @tree = Tree.new(@repo, @commit.id, @ref, params[:path])
-    @hex_path = Digest::SHA1.hexdigest(params[:path] || "")
-
-    if params[:path]
-      @logs_path = logs_file_project_ref_path(@project, @ref, params[:path])
-    else
-      @logs_path = logs_tree_project_ref_path(@project, @ref)
-    end
-  rescue
-    return render_404
-  end
-
-  def ref
-    @ref = params[:id] || params[:ref]
-  end
 end
diff --git a/app/controllers/tree_controller.rb b/app/controllers/tree_controller.rb
index a03ea3ff30d6ec30d65d0290cef99c8c7c07d18f..24e1329f926ad710be51725728f3a3f5099661c6 100644
--- a/app/controllers/tree_controller.rb
+++ b/app/controllers/tree_controller.rb
@@ -8,9 +8,6 @@ class TreeController < ProjectResourceController
   before_filter :require_non_empty_project
 
   def show
-    @hex_path  = Digest::SHA1.hexdigest(@path)
-    @logs_path = logs_file_project_ref_path(@project, @ref, @path)
-
     respond_to do |format|
       format.html
       # Disable cache so browser history works
diff --git a/app/views/graph/_head.html.haml b/app/views/graph/_head.html.haml
index fba9a958a191426605884fb2a9dbf834e43382dd..04a668925845b4358f3be14984d5416d45051fff 100644
--- a/app/views/graph/_head.html.haml
+++ b/app/views/graph/_head.html.haml
@@ -3,10 +3,10 @@
 
 .clearfix
   .pull-left
-    = render partial: 'shared/ref_switcher', locals: {destination: 'graph', path: @path}
+    = render partial: 'shared/ref_switcher', locals: {destination: 'graph'}
 
   .search.pull-right
-    = form_tag project_graph_path(@project, params[:id]), method: :get do |f|
+    = form_tag project_graph_path(@project, @id), method: :get do |f|
       .control-group
         = label_tag :search , "Looking for commit:", class: 'control-label light'
         .controls
diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb
index 1b7c698d0a87acd28d59cddd04b32d2e4d4110a2..6025bb2a9f680132faf7f5eafa156b756f7c0de8 100644
--- a/lib/extracts_path.rb
+++ b/lib/extracts_path.rb
@@ -94,16 +94,28 @@ module ExtractsPath
   # Automatically renders `not_found!` if a valid tree path could not be
   # resolved (e.g., when a user inserts an invalid path or ref).
   def assign_ref_vars
-    @id = params[:id]
+    @id = get_id
 
     @ref, @path = extract_ref(@id)
 
-    @commit = @project.repository.commit(@ref)
+    @repo = @project.repository
 
-    @tree = Tree.new(@project.repository, @commit.id, @ref, @path)
+    @commit = @repo.commit(@ref)
+
+    @tree = Tree.new(@repo, @commit.id, @ref, @path)
+    @hex_path = Digest::SHA1.hexdigest(@path)
+    @logs_path = logs_file_project_ref_path(@project, @ref, @path)
 
     raise InvalidPathError unless @tree.exists?
   rescue RuntimeError, NoMethodError, InvalidPathError
     not_found!
   end
+
+  private
+
+  def get_id
+    id = params[:id] || params[:ref]
+    id += "/" + params[:path] unless params[:path].blank?
+    id
+  end
 end