Code dump from my thesis work. Who knows whether it works -- documenting my code wasn't my first priority at the time....