diff --git a/advagg_css_compress/advagg_css_compress.module b/advagg_css_compress/advagg_css_compress.module
index 77c7627..15dc774 100644
--- a/advagg_css_compress/advagg_css_compress.module
+++ b/advagg_css_compress/advagg_css_compress.module
@@ -43,7 +43,10 @@ function advagg_css_compress_advagg_css_alter(&$contents, $files, $bundle_md5) {
   include_once($filename);
   $css = new csstidy();
 
-  set_time_limit(0);
+  // Try to allocate enough time to run CSSTidy.
+  if (function_exists('set_time_limit')) {
+    @set_time_limit(240);
+  }
 
   // Set configuration.
   $css->set_cfg('preserve_css', variable_get('advagg_css_compress_preserve_css', ADVAGG_CSS_COMPRESS_PRESERVE_CSS));
diff --git a/advagg_js_compress/advagg_js_compress.module b/advagg_js_compress/advagg_js_compress.module
index af9462c..ab95da2 100644
--- a/advagg_js_compress/advagg_js_compress.module
+++ b/advagg_js_compress/advagg_js_compress.module
@@ -183,8 +183,12 @@ function advagg_js_compress_prep_jsminplus(&$contents, $files, $bundle_md5) {
  *   array with the size before and after.
  */
 function advagg_js_compress_jsminplus(&$contents) {
+  // Try to allocate enough time to run JSMin+.
+  if (function_exists('set_time_limit')) {
+    @set_time_limit(240);
+  }
+
   // JSMin+ the contents of the aggregated file.
-  set_time_limit(0);
   require_once(drupal_get_path('module', 'advagg_js_compress') .'/jsminplus.inc');
   // Strip Byte Order Marks (BOM's) from the file, JSMin+ cannot parse these.
   $before = strlen($contents);
