Ported from darcs, refactored to multiple cabal packages