Index: core/kernel/application.php
===================================================================
--- core/kernel/application.php
+++ core/kernel/application.php
@@ -1078,17 +1078,26 @@
 	}
 
 	/**
-	 * Only renders template
+	 * Replaces current rendered template with given one.
 	 *
-	 * @see kDBEventHandler::_errorNotFound()
+	 * @param string|null $template Template.
+	 *
+	 * @return void
 	 */
-	public function QuickRun()
+	public function QuickRun($template)
 	{
-		// Replace current page content with 404.
-		$this->InitParser();
+		/** @var kThemesHelper $themes_helper */
+		$themes_helper = $this->recallObject('ThemesHelper');
 
+		// Set Web Request variables to affect link building on template itself.
+		$this->SetVar('t', $template);
+		$this->SetVar('m_cat_id', $themes_helper->getPageByTemplate($template));
+		$this->SetVar('passed', 'm');
+
+		// Replace current page content with given template.
+		$this->InitParser();
 		$this->Parser->Clear();
-		$this->HTML = $this->Parser->Run($this->GetVar('t'));
+		$this->HTML = $this->Parser->Run($template);
 	}
 
 	/**
Index: core/kernel/managers/rewrite_url_processor.php
===================================================================
--- core/kernel/managers/rewrite_url_processor.php
+++ core/kernel/managers/rewrite_url_processor.php
@@ -152,6 +152,9 @@
 				// don't cache 404 Not Found
 				$this->_setCachedUrl($url, Array ('vars' => $vars, 'passed' => $passed));
 			}
+			else {
+				header('HTTP/1.0 404 Not Found');
+			}
 
 			if ( $this->Application->GetVarDirect('t', 'Post') ) {
 				// template from POST overrides template from URL.
@@ -445,7 +448,13 @@
 		}
 
 		if ( $this->_partsToParse ) {
-			$vars = array_merge($vars, $this->manager->prepare404($vars['m_theme']));
+			/** @var kThemesHelper $themes_helper */
+			$themes_helper = $this->Application->recallObject('ThemesHelper');
+
+			$not_found = $this->Application->ConfigValue('ErrorTemplate');
+			$vars['t'] = $not_found ? $not_found : 'error_notfound';
+			$vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t'], $vars['m_theme']);
+			$vars['pass'] = array('m');
 		}
 
 		return $vars;
@@ -1078,4 +1087,4 @@
 
 		return $ret;
 	}
-}
\ No newline at end of file
+}
Index: core/kernel/managers/url_manager.php
===================================================================
--- core/kernel/managers/url_manager.php
+++ core/kernel/managers/url_manager.php
@@ -452,31 +452,6 @@
 	}
 
 	/**
-	 * Prepares case, when requested page wasn't found
-	 *
-	 * @param int $theme_id
-	 * @return array
-	 */
-	public function prepare404($theme_id = null)
-	{
-		if ( !isset($theme_id) ) {
-			$theme_id = $this->Application->GetVar('m_theme');
-		}
-
-		$not_found = $this->Application->ConfigValue('ErrorTemplate');
-		$vars['t'] = $not_found ? $not_found : 'error_notfound';
-
-		$themes_helper = $this->Application->recallObject('ThemesHelper');
-		/* @var $themes_helper kThemesHelper */
-
-		$vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t'], $theme_id);
-
-		header('HTTP/1.0 404 Not Found');
-
-		return $vars;
-	}
-
-	/**
 	 * Show 404 page and exit
 	 *
 	 * @return void
@@ -484,15 +459,13 @@
 	 */
 	public function show404()
 	{
-		$vars = $this->prepare404();
+		header('HTTP/1.0 404 Not Found');
 
-		foreach ($vars as $var_name => $var_value) {
-			$this->Application->SetVar($var_name, $var_value);
-		}
+		$not_found = $this->Application->ConfigValue('ErrorTemplate');
+		$template = $not_found ? $not_found : 'error_notfound';
 
-		// ensure parser is available (e.g. 404 page requested from event)
-		$this->Application->QuickRun();
+		$this->Application->QuickRun($template);
 		$this->Application->Done();
 		exit;
 	}
-}
\ No newline at end of file
+}
Index: core/kernel/nparser/nparser.php
===================================================================
--- core/kernel/nparser/nparser.php
+++ core/kernel/nparser/nparser.php
@@ -142,8 +142,12 @@
 
 	function Clear()
 	{
-		// Discard any half-parsed content.
-		ob_clean();
+		// Discard any half-parsed content (e.g. from nested RenderElements).
+		$keep_buffering_levels = kUtil::constOn('SKIP_OUT_COMPRESSION') ? 1 : 2;
+
+		while ( ob_get_level() > $keep_buffering_levels ) {
+			ob_end_clean();
+		}
 
 		$this->Stack = array();
 		$this->Level = 0;
Index: core/units/categories/categories_event_handler.php
===================================================================
--- core/units/categories/categories_event_handler.php
+++ core/units/categories/categories_event_handler.php
@@ -1766,17 +1766,7 @@
 				}
 			}
 			else {
-				$not_found = $this->Application->ConfigValue('ErrorTemplate');
-				$real_t = $not_found ? $not_found : 'error_notfound';
-
-				$themes_helper = $this->Application->recallObject('ThemesHelper');
-				/* @var $themes_helper kThemesHelper */
-
-				$theme_id = $this->Application->GetVar('m_theme');
-				$category_id = $themes_helper->getPageByTemplate($real_t, $theme_id);
-				$this->Application->SetVar('m_cat_id', $category_id);
-
-				header('HTTP/1.0 404 Not Found');
+				$this->Application->UrlManager->show404();
 			}
 
 			// replace alias in form #alias_name# to actual template used in this theme