diff --git a/Configure.PL b/Configure.PL
new file mode 100644
--- /dev/null
+++ b/Configure.PL
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+
+use 5.006;
+use strict;
+use Cwd;
+use Config;
+use ExtUtils::Embed;
+
+my $embed_flags = "-I" . cwd();
+my $ccdlflags = "";
+my $flags = "$Config{ccflags} $Config{ccdlflags} ";
+
+if ($flags =~ /\S/) {
+    $flags =~ s{([\\"'])}{\\$1}g;
+    my @flags = grep { length $_ } split /\s+/, $flags;
+
+    if ($^O eq 'MSWin32') {
+        if ($Config{libperl} =~ /lib(\w+)\.a/) {
+            $embed_flags .= " -optl-l$1 ";
+        }
+        elsif (defined &Win32::BuildNumber) {
+            # We are on ActivePerl -- Kluge massively!
+
+            no warnings 'once';
+            our %MY_CONFIG = %Config;
+            *Config = *MY_CONFIG;
+            *Config::Config = *MY_CONFIG;
+            *ExtUtils::MM_Win32::Config = *MY_CONFIG;
+            *ExtUtils::MM_Unix::Config = *MY_CONFIG;
+
+            $Config{ccflags} =~ s/-libpath:"?(.*?)"? //g;
+            $Config{ccdlflags} =~ s/-libpath:"?(.*?)"? //g;
+            $Config{lddlflags} =~ s/-libpath:"?(.*?)"? //g;
+            $Config{ldflags} =~ s/-libpath:"?(.*?)"? //g
+                or die "ldflags: $Config{ldflags} does not contain -libpath:";
+
+            my $lib = "$1/$Config{libperl}";
+            $embed_flags .= " -optl\"$lib\" ";
+
+            $flags = "$Config{ccflags} $Config{ccdlflags}";
+            $flags =~ s{([\\"'])}{\\$1}g;
+            @flags = grep { length $_ } split /\s+/, $flags;
+        }
+        else {
+            warn "Unrecognized libperl shared library: $Config{libperl}, proceeding anyway...\n";
+        }
+
+        $ccdlflags .= (/^-[DIL]/ ? ' -optc' : ' -optl') . qq["$_" ] for @flags;
+        $embed_flags .= " -optc-Ddirent=DIRENT";
+    }
+    else {
+        $embed_flags .= " -optc$_" for grep length, split(/\s+/, ccopts());
+        $embed_flags .= " -optl$_" for grep length, split(/\s+/, ldopts());
+    }
+
+    $embed_flags .= " $_" for grep { /-[DIL]/ } split(/\s+/, ccopts());
+    $embed_flags .= " $_" for grep { /-[DIL]/ } split(/\s+/, ldopts());
+
+    if ($Config{osname} eq 'cygwin') {
+        my $cygpath = sub {
+            my $path = `cygpath -m @_`;
+            chomp $path;
+            return $path;
+        };
+        $embed_flags =~ s{(/usr/\S+)}{$cygpath->($1)}eg;
+        $embed_flags =~ s{/cygdrive/(\w)/}{$1:/}g;
+        #warn "** Cygwin embedding flags: embed_flags\n";
+    }
+}
+
+my @include_dirs = split(/\s+/, perl_inc());
+s/^-I// for @include_dirs;
+
+my @cc_options = map { /^-optc(.+)/ ? $1 : () } (split(/\s+/, $embed_flags), split(/\s+/, $ccdlflags));
+my @ld_options = map { /^-optl(.+)/ ? $1 : () } (split(/\s+/, $embed_flags), split(/\s+/, $ccdlflags));
+
+open INFO, ">HsPerl5.buildinfo" or die "Cannot write build info: $!";
+print INFO << ".";
+ghc-options: $embed_flags $ccdlflags
+include-dirs: @include_dirs
+cc-options: @cc_options
+ld-options: @ld_options
+.
+close INFO;
+
+xsinit();
diff --git a/HsPerl5.cabal b/HsPerl5.cabal
--- a/HsPerl5.cabal
+++ b/HsPerl5.cabal
@@ -1,5 +1,5 @@
 name:               HsPerl5
-version:            0.0.5
+version:            0.0.6
 copyright:          2008 Audrey Tang
 license:            BSD3
 license-file:       LICENSE
@@ -22,13 +22,13 @@
                     carried into this module.
 
 stability:          experimental
-build-type:         Simple
+build-type:         Custom
 extensions:         ForeignFunctionInterface, TypeSynonymInstances,
                     ScopedTypeVariables, FlexibleInstances,
                     UndecidableInstances, OverlappingInstances, IncoherentInstances
 exposed-modules:    Language.Perl5
 build-depends:      base
-extra-source-files: README test.hs configure p5embed.h
+extra-source-files: README test.hs Configure.PL p5embed.h perlxsi.c
 hs-source-dirs:     src
 category:           Language, Pugs
 c-sources:          p5embed.c
diff --git a/Setup.lhs b/Setup.lhs
--- a/Setup.lhs
+++ b/Setup.lhs
@@ -1,3 +1,8 @@
 #!/usr/bin/env runghc
 > import Distribution.Simple
-> main = defaultMainWithHooks defaultUserHooks
+> import System.Cmd (rawSystem)
+> 
+> main :: IO ()
+> main = writeBuildInfo >> defaultMainWithHooks defaultUserHooks
+>     where
+>     writeBuildInfo = rawSystem "perl" ["Configure.PL"]
diff --git a/configure b/configure
deleted file mode 100644
--- a/configure
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/sh -e
-
-perl << CONFIGURE
-
-use ExtUtils::Embed;
-
-my @ccoptions   = (split(/\s+/, ccflags()), split(/\s+/, ccdlflags()));
-my @includedirs = split(/\s+/, perl_inc());
-my @ldoptions   = split(/\s+/, ldopts());
-s/^-I// for @includedirs;
-
-my \$info = << ".";
-cc-options: @ccoptions
-ld-options: @ldoptions
-include-dirs: @includedirs
-.
-
-\$info =~ s/-arch\s+\w+\s*//g;
-
-open INFO, ">HsPerl5.buildinfo" or die "Cannot write build info";
-print INFO \$info;
-close INFO;
-
-xsinit();
-
-CONFIGURE
diff --git a/perlxsi.c b/perlxsi.c
new file mode 100644
--- /dev/null
+++ b/perlxsi.c
@@ -0,0 +1,16 @@
+#include <EXTERN.h>
+#include <perl.h>
+
+EXTERN_C void xs_init (pTHX);
+
+EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
+
+EXTERN_C void
+xs_init(pTHX)
+{
+	char *file = __FILE__;
+	dXSUB_SYS;
+
+	/* DynaLoader is a special case */
+	newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
+}
