diff --git a/Library/Homebrew/brew.h.rb b/Library/Homebrew/brew.h.rb
index 0ec3e2c002d5f8ac9843f8ed51774ee96ec60c5d..7a5e55fea0864b01a97e161d051b572e32b16344 100644
--- a/Library/Homebrew/brew.h.rb
+++ b/Library/Homebrew/brew.h.rb
@@ -229,6 +229,23 @@ def diy
   end
 end
 
+
+def fix_PATH
+  bad_paths  = `/usr/bin/which -a port`.split
+  bad_paths += `/usr/bin/which -a fink`.split
+
+  # don't remove standard paths!
+  bad_paths.delete_if do |pn|
+    %w[/usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/X11/bin].include? pn or pn.empty?
+  end
+  bad_paths += %w[/opt/local/bin /opt/local/sbin /sw/bin /sw/sbin]
+
+  paths = ENV['PATH'].split(':').reject do |p|
+    p.squeeze! '/'
+    bad_paths.find { |pn| p =~ /^#{pn}/ } and true
+  end
+  ENV['PATH'] = paths*':'
+end
 ################################################################ class Cleaner
 class Cleaner
   def initialize f
diff --git a/Library/Homebrew/brewkit.rb b/Library/Homebrew/brewkit.rb
index be22ca6755e39fb9db51252290b1c1b320c60f49..132bda851e9b09c59362ff4d42ede9e763686bbf 100644
--- a/Library/Homebrew/brewkit.rb
+++ b/Library/Homebrew/brewkit.rb
@@ -175,14 +175,6 @@ end
 ENV.extend HomebrewEnvExtension
 
 
-# remove MacPorts and Fink from the PATH, this prevents issues like:
-# http://github.com/mxcl/homebrew/issues/#issue/13
-paths=ENV['PATH'].split(':').reject do |p|
-  p.squeeze! '/'
-  p =~ %r[^/opt/local] or p =~ %r[^/sw]
-end
-ENV['PATH']=paths*':'
-
 # Clear CDPATH to avoid make issues that depend on changing directories
 ENV.delete('CDPATH')
 
diff --git a/bin/brew b/bin/brew
index 31e5408480b76f59eeb97a9435d49dc1f9404fed..5664ee5a745fcfe40eab1d3965e244e89339b66f 100755
--- a/bin/brew
+++ b/bin/brew
@@ -12,6 +12,12 @@ if %w[/ /usr].include? HOMEBREW_PREFIX.to_s then abort <<-EOS
   EOS
 end
 if `sw_vers -productVersion` =~ /10\.(\d)\.(\d+)/ and $1.to_i < 5
+
+# remove MacPorts and Fink from the PATH, this prevents issues like:
+# http://github.com/mxcl/homebrew/issues/#issue/13
+# http://github.com/mxcl/homebrew/issues/#issue/48
+fix_PATH
+
   onoe "Homebrew requires Leopard or higher"
   abort "But thanks for your interest anyway!"
 end