basic module file generation/loading
diff --git a/lib/mako/lexer.py b/lib/mako/lexer.py
index f4c044d..f3c5941 100644
--- a/lib/mako/lexer.py
+++ b/lib/mako/lexer.py
@@ -226,4 +226,4 @@
             return False
             
     def _count_lines(self, text):
-        return len(re.findall(r"\n", text)) 
\ No newline at end of file
+        return len(re.findall(r"\n", text)) 
diff --git a/lib/mako/lookup.py b/lib/mako/lookup.py
index 44cfd2a..d1e388e 100644
--- a/lib/mako/lookup.py
+++ b/lib/mako/lookup.py
@@ -29,7 +29,7 @@
         self.module_directory = module_directory
         self.filesystem_checks = filesystem_checks
         self.collection_size = collection_size
-        self.template_args = {'format_exceptions':format_exceptions, 'error_handler':error_handler, 'output_encoding':output_encoding}
+        self.template_args = {'format_exceptions':format_exceptions, 'error_handler':error_handler, 'output_encoding':output_encoding, 'module_directory':module_directory}
         if collection_size == -1:
             self.__collection = {}
         else:
@@ -62,7 +62,7 @@
         
     def __load(self, filename, uri):
         try:
-            self.__collection[uri] = Template(file(filename).read(), identifier=self.__ident_from_uri(uri), description=uri, filename=filename, lookup=self, **self.template_args)
+            self.__collection[uri] = Template(identifier=self.__ident_from_uri(uri), description=uri, filename=filename, lookup=self, **self.template_args)
             return self.__collection[uri]
         except:
             self.__collection.pop(uri, None)
diff --git a/lib/mako/template.py b/lib/mako/template.py
index 34e9bcf..1787c01 100644
--- a/lib/mako/template.py
+++ b/lib/mako/template.py
@@ -9,9 +9,8 @@
 
 from mako.lexer import Lexer
 from mako.codegen import Compiler
-from mako import runtime
-from mako import util
-import imp, time, weakref, tempfile, shutil
+from mako import runtime, util, exceptions
+import imp, time, weakref, tempfile, shutil,  os, stat, posixpath, sys
 
 _modules = weakref.WeakValueDictionary()
 _inmemory_templates = weakref.WeakValueDictionary()
@@ -45,13 +44,14 @@
         elif filename is not None:
             if module_directory is not None:
                 path = posixpath.join(module_directory, identifier + ".py")
-                if not os.access(path, os.F_OK):
+                filemtime = os.stat(filename)[stat.ST_MTIME]
+                if not os.access(path, os.F_OK) or os.stat(path)[stat.ST_MTIME] < filemtime:
                     util.verify_directory(module_directory)
-                    _compile_module(text, identifier, filename, module_directory)
+                    _compile_module_file(file(filename).read(), identifier, filename, path)
                 module = imp.load_source(self.identifier, path, file(path))
                 del sys.modules[self.identifier]
             else:
-                (code, module) = _compile_text(file(filename), self.identifier, filename)
+                (code, module) = _compile_text(file(filename).read(), self.identifier, filename)
                 self._source = None
                 self._code = code
         else:
@@ -115,15 +115,13 @@
     exec code in module.__dict__, module.__dict__
     return (source, module)
 
-def _compile_module(text, identifier, filename, outputpath):
-    
-    dest = tempfile.NamedTemporaryFile()
-
+def _compile_module_file(text, identifier, filename, outputpath):
+    (dest, name) = tempfile.mkstemp()
     node = Lexer(text, filename).parse()
     source = Compiler(node, filename).render()
-    dest.write(source)
-    dest.close()
-    shutil.move(dest.name, outputpath)
+    os.write(dest, source)
+    os.close(dest)
+    shutil.move(name, outputpath)
 
 def _get_template_source(callable_):
     """return the source code for the template that produced the given rendering callable"""