Fixed queries in templates.php