diff --git a/Cake/Core.hs b/Cake/Core.hs
--- a/Cake/Core.hs
+++ b/Cake/Core.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE GeneralizedNewtypeDeriving, TypeSynonymInstances, TemplateHaskell, RecordWildCards  #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving, TypeSynonymInstances, TemplateHaskell, RecordWildCards, FlexibleInstances  #-}
 
 module Cake.Core (
   -- * Patterns and rules.
@@ -12,10 +12,13 @@
   list,
   -- * Mid-level interface
   produce,
+  produce',
   use,
+  overwrote,
   -- * Low-level interface
   debug,
   distill,
+  fileStamp,
   cut,
   shielded,
   Question(..),
@@ -116,33 +119,54 @@
   (ps,_) <- RWS.get
   return $ f `S.member` ps
 
+-- | Answer a question using the action given.
+-- The action should be independent of the context.
 distill :: Question -> Act Answer -> Act Answer
-distill q act = local modCx $ do
-  debug $ "Starting to answer"
-  db <- ctxDB <$> ask
-  let a0 = M.lookup q db
-  a1 <- shielded act
-  tell (Dual $ M.singleton q a1)
-  when (Just a1 /= a0) $ do
-    clobber
-    debug $ "Question has not the same answer"
+distill q act = do
+  a1 <- local (modCx q) $ do
+    debug $ "Starting to answer"
+    db <- ctxDB <$> ask
+    let a0 = M.lookup q db
+    a1 <- shielded act
+    tell (Dual $ M.singleton q a1)
+    when (Just a1 /= a0) $ do
+      clobber
+      debug $ "Question has not the same answer"
+    return a1
+  debug $ "..."
   return a1
- where modCx (Context {..}) = Context {ctxProducing = q:ctxProducing,..}    
 
+modCx q (Context {..}) = Context {ctxProducing = q:ctxProducing,..}    
 
+refresh :: Question -> Act Answer -> Act Answer
+refresh q act = local (modCx q) $ do
+   debug $ "Overwriting"
+   a <- shielded act
+   tell (Dual $ M.singleton q a)
+   return a
+   
+-- | Produce a file, using with the given action.
+-- The action should be independent of the context.
 produce :: FilePath -> Act () -> Act ()
 produce f a = do
   p <- produced f -- Do nothing if the file is already produced.
   when (not p) $ do
-     distill (FileContents f) $ 
-       do e <- liftIO $ doesFileExist f
+     produce' f a
+     return ()
+
+-- | Produce a file, using with the given action.
+-- The action should be independent of the context.
+-- BUT: no problem to produce the same file multiple times.
+produce' :: FilePath -> Act () -> Act Answer
+produce' f a = distill (FileContents f) $ do
+          e <- liftIO $ doesFileExist f
           when (not e) clobber 
           a
           modify $ first $ S.insert f 
           -- remember that the file has been produced already
           fileStamp f
-     return ()
 
+
 -- | List directory contents by extension
 list directory extension = do 
   Text x <- distill (Listing directory extension) $ do
@@ -156,7 +180,12 @@
 -- though.
 use f = do
   distill (FileContents f) (fileStamp f)
-  
+
+
+-- | File was modified by some command, but in a way that does not
+-- invalidate previous computations. (This is probably only useful for
+-- latex processing).
+overwrote f = refresh (FileContents f) (fileStamp f)
 
 -- | Run the argument in a clean context, and do not clobber the state
 -- even if the argument does that.  To use when the construction of
diff --git a/Cake/Rules.hs b/Cake/Rules.hs
--- a/Cake/Rules.hs
+++ b/Cake/Rules.hs
@@ -104,34 +104,43 @@
             (l:_) -> splitOn "," $ reverse . dropWhile (== '}') . reverse $ l
     return $ Text bibs
 
-chaseDeps input = do
+includedTex input = do
     ls <- map (drop 7) . filter ("\\input{" `isPrefixOf`) . lines <$> Cake.Rules.readFile input
     let inputs = map (++".tex") $ case ls of
             [] -> []
             (l:_) -> splitOn "," $ reverse . dropWhile (== '}') . reverse $ l
-    return $ input:inputs
+    return $ inputs
 
+chaseDeps i = do
+  is <- includedTex i
+  mapM_ chaseDeps is
+  
 
 pdflatexBibtex c = do
   let input = c ++ ".tex"
-      aux1 = c ++ ".aux1"
       aux = c ++ ".aux"
       pdf = c ++ ".pdf"
+      bbl = c ++ ".bbl"
   produce pdf $ do 
-    inputs <- chaseDeps input
-    produce aux1 $ needing inputs $ do _pdflatex c
-                                       liftIO $ copyFile aux aux1
-    produce aux $ do
-      produce (c ++ ".bbl") $ do
+        
+    -- hack: if nothing changed in the aux file, there is no need to
+    -- do the final pdflatex, because pdflatex has already produced the tex file.
+    produce' aux $ do
+      produce bbl $ do
+        produce' aux $ do
+          chaseDeps input
+          cut $ _pdflatex c
+
         Text bibs <- getBibFiles input
         -- Note that this does not depend on the actual tex file; only the list of bibs. (aux1)
         mapM_ need bibs
-        use aux1
         when (not $ null bibs) $
           cut $ _bibtex c
       cut $ _pdflatex c
     
-    cut $ _pdflatex c
+    cut $ do _pdflatex c
+             overwrote aux
+             return ()
 
 pdf_tex_bibtex = extension ".pdf" ==> \(_,c) -> pdflatexBibtex c
 
@@ -140,7 +149,9 @@
       aux = c ++ ".aux"
       pdf = c ++ ".pdf"
   produce pdf $ do
-    produce aux $ needing [input] $ _pdflatex c
+    produce aux $ do 
+      chaseDeps input
+      cut $ _pdflatex c
     
     produce (c ++ ".bbl") $ do
       -- Note that this does not depend on the actual tex file; only the list of bibs.
@@ -150,10 +161,27 @@
       when (not $ null bibs) $
         cut $ _bibtex c
 
-    cut $ _pdflatex c
+    cut $ do _pdflatex c
+             overwrote aux
+             return ()
     
 
 pdf_tex_biblatex = anyExtension [".pdf",".aux"] ==> \(_,c) -> 
   pdflatexBibtex c
 
-allRules = tex_markdown_standalone <|> pdf_tex_biblatex
+
+_lhs2TeX i o = do
+  system ["lhs2TeX","-o",o,i]
+
+lhs2tex c = do
+  let lhs = c ++ ".lhs"
+      tex = c ++ ".tex"
+  produce tex $ do
+    need lhs
+    _lhs2TeX lhs tex
+
+tex_lhs = extension ".tex" ==> \(_,c) -> lhs2tex c
+
+allRules = tex_markdown_standalone 
+         <|> pdf_tex_biblatex
+         <|> tex_lhs
diff --git a/Main.hs b/Main.hs
--- a/Main.hs
+++ b/Main.hs
@@ -8,6 +8,7 @@
 import Control.Applicative
 import Data.Array
 import System.FilePath
+import System.Directory
 
 data Args = Args {targets' :: [String],
                   rules :: String,
@@ -31,8 +32,7 @@
 
 define (modul,dir) = "-DROOT_" ++ modul ++ "=" ++ show (show dir)
 
-main = do  
-  Args {..} <- cmdArgsRun opts
+mainCake Args{..} = do  
   cf <- readFile cakefile
   
   let 
@@ -45,4 +45,13 @@
   putStrLn $ "imports: " ++ show imports
   putStrLn $ "cake: running " ++ command
   system command
+
+mainCabal Args{..} = do
+  putStrLn $ cakefile ++ " not found, running cabal install"
+  system "cabal install"
+
+main = do
+  Args {..} <- cmdArgsRun opts
+  e <- doesFileExist cakefile
+  (if e then mainCake else mainCabal) Args{..}
   return ()
diff --git a/cake.cabal b/cake.cabal
--- a/cake.cabal
+++ b/cake.cabal
@@ -1,5 +1,5 @@
 name:           cake
-version:        0.3.0
+version:        0.3.2.1
 category:       Development
 synopsis:       A build-system library and driver
 description:
@@ -8,17 +8,19 @@
 license-file:   LICENSE
 author:         Jean-Philippe Bernardy
 maintainer:     jeanphilippe.bernardy@gmail.com
-Cabal-Version:  >= 1.8
+Cabal-Version:  >= 1.12
 tested-with:    GHC==6.12.1
+tested-with:    GHC==7.4.1
 build-type:     Simple
 
 library
+  default-language: Haskell2010
   --  extensions:  PackageImports, GeneralizedNewtypeDeriving, TypeSynonymInstances
   build-depends: base==4.*
   build-depends: derive==2.5.*
-  build-depends: mtl==2.0.*
-  build-depends: filepath==1.2.*  
-  build-depends: process==1.0.*
+  build-depends: mtl==2.1.*
+  build-depends: filepath==1.3.*  
+  build-depends: process==1.1.*
   build-depends: binary==0.5.*
   build-depends: containers==0.4.*
   build-depends: directory==1.1.*
@@ -26,16 +28,17 @@
   build-depends: pureMD5==2.1.*
   build-depends: split==0.1.*
 
-  exposed-modules: Cake, Cake.Core, Cake.Rules, Cake.Process
-  other-modules: Parsek
+  exposed-modules: Cake, Cake.Core, Cake.Rules, Cake.Process, Parsek
 
 executable cake
+  default-language: Haskell2010
   build-depends: base==4.*
-  build-depends: process==1.0.*
-  build-depends: cmdargs==0.7.*
+  build-depends: process==1.1.*
+  build-depends: cmdargs==0.9.*
   build-depends: regex-tdfa==1.1.*
-  build-depends: array==0.3.*
-  build-depends: filepath==1.2.*  
+  build-depends: array==0.4.*
+  build-depends: filepath==1.3.*  
+  build-depends: directory==1.1.*
 
   main-is: Main.hs
 
