--- /sites/all/modules/contributed/twitter_pull/twitter_pull.module
+++ /sites/all/modules/contributed/twitter_pull/twitter_pull.module
@@ -6,10 +6,11 @@
  * Twitter Pull module.
  */
 
-require_once (dirname(__FILE__) . '/twitter_pull.class.inc');
+require_once (dirname(__FILE__) .'/twitter_pull.class.inc');
 
-define ('TWITTER_PULL_NUM_ITEMS', 5);
-define ('TWITTER_PULL_CACHE_LENGTH', 20); //-- cache for 20 minutes
+define ('TWITTER_PULL_NUM_ITEMS', 6);
+define ('TWITTER_PULL_CACHE_LENGTH', 0); //-- Refresh every 20 mins, or 0 for CACHE_PERMANENT
+define ('TWITTER_PULL_REFRESH_DELAY', 1); //-- Refresh every 20 mins
 define ('TWITTER_PULL_EMPTY_MESSAGE', "No Tweets");
 
 define ('TWITTER_PULL_CACHE_TABLE', 'cache_pulled_tweets');
@@ -22,6 +23,10 @@
   return variable_get('twitter_pull_cache_length', TWITTER_PULL_CACHE_LENGTH);
 }
 
+function twitter_pull_refresh_delay() {
+  return variable_get('twitter_pull_refresh_delay', TWITTER_PULL_REFRESH_DELAY);
+}
+
 function twitter_pull_empty_message() {
   return variable_get('twitter_pull_empty_message', TWITTER_PULL_EMPTY_MESSAGE);
 }
@@ -30,10 +35,15 @@
 * Implementation of hook_flush_caches;
 */
 function twitter_pull_flush_caches() {
-  return array(TWITTER_PULL_CACHE_TABLE);
+  
+  // Only flush content needing to expire from cache
+  cache_clear_all(NULL, TWITTER_PULL_CACHE_TABLE);
+
 }
 
 
+
+
 /**
 * Retrieves appropriate tweets (by username, hashkey or search term) and passes over to the theming
 * function with $themekey key passing tweets array along. 
@@ -50,28 +60,53 @@
 * @param $themekey
 *     theme key name to use for theming the output of twitter API.
 */
-function twitter_pull_render ($twitkey, $title = NULL, $num_items = NULL, $themekey = NULL) {
+function twitter_pull_render($twitkey, $title = NULL, $num_items = NULL, $themekey = NULL) {
 
   //-- Set defaults if empty arguments were passed
   $title = empty($title) ? t('Related Tweets') : $title;
   $themekey = empty($themekey) ? 'twitter_pull_listing' : $themekey;
   $num_items = empty($num_items) ? twitter_pull_num_items() : $num_items;
 
-  $cache_key = $twitkey . '::' . $themekey;
+  $cache_key = $twitkey .'::'. $themekey;
+
   $cache = cache_get($cache_key, TWITTER_PULL_CACHE_TABLE);
-  
+
   $tweets = array();
   
-  if (!empty($cache) && !empty($cache->data)) {
-    $tweets =  $cache->data;
-  }
-  else {
+  $refresh_delay = twitter_pull_refresh_delay();
+  
+  $refresh = $refresh_delay && ($cache->created < (time() - ($refresh_delay * 60)));
+
+  if (empty($cache) || empty($cache->data) || $refresh) {
     
     try {
       
       $puller = new twitter_puller($twitkey, $themekey, $num_items);
       $puller->get_items();
-      $tweets = $puller->tweets;    
+      $tweets = $puller->tweets;
+
+      // Pad out tweets with items from the cache
+      if (($count = ($num_items - count($tweets))) && $cache->data) {
+        
+        $i = 0;
+        
+        $tweet_ids = array_map('twitter_pull_map_ids', $tweets);
+        
+        while ($count > 0 && $i < count($cache->data)) {
+          
+          if (!in_array($cache->data[$i]->id, $tweet_ids)) {
+            
+            $tweets[] = $cache->data[$i];          
+            $count--;
+            
+          }
+          
+          $i++;
+          
+        }
+
+
+      }  
       
     } catch (Exception $e) {
       watchdog('Twitter Pull', $e->getMessage(), array(), WATCHDOG_WARNING);
@@ -79,11 +114,29 @@
     }
     
     if (!empty($tweets) && is_array($tweets)) {
-      $cache_length = twitter_pull_cache_length() * 60; //-- in the settings we indicate length in minutes, here we need seconds.
-      cache_set($cache_key, $tweets, TWITTER_PULL_CACHE_TABLE, time() + $cache_length);
+      
+      if ($cache_length = twitter_pull_cache_length()) {
+        
+        $cache_length *= 60;
+        $cache_length += time();
+        
+      }
+      else{
+        
+        $cache_length = CACHE_PERMANENT;
+        
+      } 
+      
+      cache_set($cache_key, $tweets, TWITTER_PULL_CACHE_TABLE, $cache_length);
+      
     }
-
+    
   }
+  else{
+    
+    $tweets = $cache->data;
+    
+  }
 
   $ret = theme($themekey, $tweets, $twitkey, $title);
   
@@ -98,6 +151,12 @@
   
 }
 
+function twitter_pull_map_ids($cached_data) {
+  
+  return $cached_data->id;
+  
+}
+
 /**
 * Automatically add links to hrefs and twitter usernames in a twitter message.
 */
