diff --git a/tsocks b/tsocks
index 8380d9c..7d1df7a 100755
--- a/tsocks
+++ b/tsocks
@@ -46,14 +46,14 @@ case "$1" in
 	on)
 		if [ -z "$LD_PRELOAD" ]
 			then
-				export LD_PRELOAD="/usr/lib/libtsocks.so"
+				export LD_PRELOAD="$PREFIX/lib/libtsocks.so"
 			else
-				echo $LD_PRELOAD | grep -q "/usr/lib/libtsocks\.so" || \
-				export LD_PRELOAD="/usr/lib/libtsocks.so $LD_PRELOAD"
+				echo $LD_PRELOAD | grep -q "$PREFIX/lib/libtsocks\.so" || \
+				export LD_PRELOAD="$PREFIX/lib/libtsocks.so $LD_PRELOAD"
 		fi
 	;;
 	off)
-		export LD_PRELOAD=`echo -n $LD_PRELOAD | sed 's/\/usr\/lib\/libtsocks.so \?//'`
+		export LD_PRELOAD=`echo -n $LD_PRELOAD | sed "s:$PREFIX/lib/libtsocks.so *::"`
 		if [ -z "$LD_PRELOAD" ]
 			then
 				unset LD_PRELOAD
@@ -68,15 +68,15 @@ case "$1" in
 	*)
 		if [ -z "$LD_PRELOAD" ]
 		then
-			export LD_PRELOAD="/usr/lib/libtsocks.so"
+			export LD_PRELOAD="$PREFIX/lib/libtsocks.so"
 		else
-			echo $LD_PRELOAD | grep -q "/usr/lib/libtsocks\.so" || \
-			export LD_PRELOAD="/usr/lib/libtsocks.so $LD_PRELOAD"
+			echo $LD_PRELOAD | grep -q "$PREFIX/lib/libtsocks\.so" || \
+			export LD_PRELOAD="$PREFIX/lib/libtsocks.so $LD_PRELOAD"
 		fi
 
 		if [ $# = 0 ]
 		then
-			${SHELL:-/bin/sh}
+			${SHELL:-$PREFIX/bin/sh}
 		fi
 
 		if [ $# -gt 0 ]
