Fix buildsystem

Here are some patches to fix the build system that I've made in the process of getting this into Debian.