diff --git a/server.py b/server.py
index c7bca4b85a82d14ffb964ec8e45c2267657a25e5..9520a0dc5e26d01c52f2f12bb5b3f3c1c4f8fe39 100755
--- a/server.py
+++ b/server.py
@@ -22,14 +22,12 @@ config.from_pyfile('config.py', silent=True)
 
 from db import query, searchquery, ldapauth, ldapget
 
-app.jinja_env.globals['videoprefix'] = config['VIDEOPREFIX']
 mod_endpoints = []
 
+@app.template_global()
 def ismod(*args):
 	return ('user' in session)
 
-app.jinja_env.globals['ismod'] = ismod
-
 def mod_required(func):
 	mod_endpoints.append(func.__name__)
 	@wraps(func)
@@ -50,6 +48,18 @@ def register_navbar(name, icon=None):
 		return func
 	return wrapper
 
+def render_endpoint(endpoint, flashtext=None, **kargs):
+	if flashtext:
+		flash(flashtext)
+	# request.endpoint is used for navbar highlighting
+	if request.url_rule:
+		request.url_rule.endpoint = endpoint
+	return app.view_functions[endpoint](**kargs)
+
+@app.errorhandler(404)
+def handle_not_found(e):
+	return render_endpoint('index', 'Diese Seite existiert nicht!')
+
 @app.route('/')
 @register_navbar('Home', icon='home')
 def index():
@@ -72,7 +82,7 @@ def videos():
 		if course['semester'] == '':
 			course['semester'] = 'zeitlos'
 	groupedby = request.args.get('groupedby')
-	if groupedby not in ['title','semester','organizer']:
+	if groupedby not in ['title', 'semester', 'organizer']:
 		groupedby = 'semester'
 	return render_template('course.html', courses=courses, groupedby=groupedby)
 
@@ -80,8 +90,7 @@ def videos():
 def course(id):
 	courses = query('SELECT * FROM courses WHERE ((handle = ?) or id = ?) AND (? OR visible)', id, id, ismod())
 	if not courses:
-		flash('Diese Veranstaltung existiert nicht!')
-		return app.view_functions['videos'](), 404
+		return render_endpoint('index', 'Diese Veranstaltung existiert nicht!'), 404
 	lectures = query('SELECT * FROM lectures WHERE course_id = ? AND (? OR visible)', courses[0]['id'], ismod())
 	videos = query('''
 			SELECT videos.*, (videos.downloadable AND courses.downloadable) as downloadable, formats.description AS format_description
@@ -104,14 +113,12 @@ def play(id):
 	lectures = query('SELECT * FROM lectures WHERE id = ? AND (? OR visible)', id, ismod())
 	videos = query('SELECT * FROM videos WHERE lecture_id = ? AND (? OR visible)', id, ismod())
 	if not lectures:
-		flash('Diese Vorlesung existiert nicht!')
-		return app.view_functions['videos'](), 404
+		return render_endpoint('videos', 'Diese Vorlesung existiert nicht!'), 404
 	if not videos:
 		flash('Zu dieser Vorlesung wurden noch keine Videos veröffentlicht!')
 	courses = query('SELECT * FROM courses WHERE id = ? AND (? OR (visible AND listed))', lectures[0]['course_id'], ismod())
 	if not courses:
-		flash('Diese Veranstaltung existiert nicht!')
-		return app.view_functions['videos'](), 404
+		return render_endpoint('videos', 'Diese Veranstaltung existiert nicht!'), 404
 	return render_template('play.html', course=courses[0], lecture=lectures[0], videos=videos)
 
 @app.route('/search')
diff --git a/templates/base.html b/templates/base.html
index 7996514e0afcbfd0f6706d8373100a31c591be44..726ec229268b803ca2aea1f15fc631b9f65d0d7e 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -49,7 +49,7 @@
 									{% for endpoint, caption, gly, visible in navbar %}
 									{% if visible or ismod() %}
 									 <li{% if endpoint == request.endpoint %} class="active"{% endif %}>
-										 <a href="{{ url_for(endpoint)|e }}">{% if gly != '' %}<span class="glyphicon glyphicon-{{ gly }}"></span> {% endif %}{{ caption }}</a>
+										 <a href="{{ url_for(endpoint) }}">{% if gly != '' %}<span class="glyphicon glyphicon-{{ gly }}"></span> {% endif %}{{ caption }}</a>
 									</li>
 									{% endif %}
 									{% endfor %}
@@ -63,12 +63,12 @@
 													{
 														html:true,
 														title:'Login für Moderatoren',
-														content:'<form method="post" action="{{url_for('login')}}"><input placeholder="User" name="user" type="text"><br><input placeholder="Password" name="password" type="password"><br><input type="hidden" name="ref" value="{{ request.url|e }}"><input type="submit" value="Login"></form>'
+														content:'<form method="post" action="{{url_for('login', ref=request.url)}}"><input placeholder="User" name="user" type="text"><br><input placeholder="Password" name="password" type="password"><br><input type="submit" value="Login"></form>'
 													}
 											)
 										</script>
 										{% else %}
-										<a href="{{url_for('logout')}}?ref={{ request.url|urlencode }}">
+										<a href="{{url_for('logout', ref=request.url)}}">
 											{{ session.user.givenName }}
 											<span class="glyphicon glyphicon-log-out"></span>
 										</a>
diff --git a/templates/macros.html b/templates/macros.html
index 8bcb597e8c28684c839b0e85692c9bf0e0aeda4b..0e1016a8a36fd10283a6f813ec450e310738a705 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -3,7 +3,7 @@
 <li class="list-group-item">
 	<a class="hidden-xs" href="{{url_for('play', id=lecture['id'])}}" title="{{ lecture['coursetitle'] }}">
 		<div class="row">
-			<img class="col-xs-4" src="{{ videoprefix }}/{{ lecture['titlefile'] }}" alt="Vorschaubild">
+			<img class="col-xs-4" src="{{ config.VIDEOPREFIX }}/{{ lecture['titlefile'] }}" alt="Vorschaubild">
 			<div class="col-xs-4">
 				<span style="color: #000;"><strong>{{ lecture['short'] }}</strong></span><br>
 				<span style="color: #000;">{{ lecture['time'] }}</span>
@@ -19,7 +19,7 @@
 	</a>
 	<a class="visible-xs" href="{{url_for('play', id=lecture['id'])}}" title="{{ lecture['coursetitle'] }}">
 		<div class="row">
-			<img class="col-xs-12" src="{{ videoprefix }}/{{ lecture['titlefile'] }}" alt="Vorschaubild">
+			<img class="col-xs-12" src="{{ config.VIDEOPREFIX }}/{{ lecture['titlefile'] }}" alt="Vorschaubild">
 		</div>
 		<div class="row">
 			<div class="col-xs-12">
@@ -54,7 +54,7 @@
 <link rel="stylesheet" href="{{url_for('static', filename='mediaelementjs/mediaelementplayer.css')}}" />
 <video class="mejs-player" width="640" height="360" style="width: 100%; height: 100%;">
 	{% for v in videos %}
-		<source type="video/mp4" src="{{ videoprefix }}/{{ v.path }}" />
+		<source type="video/mp4" src="{{ config.VIDEOPREFIX }}/{{ v.path }}" />
 	{% endfor %}
 </video>
 <script>
@@ -94,13 +94,13 @@
 <button class="btn btn-primary dropdown-toggle {% if videos|length is equalto 0 %}disabled{% endif %}" type="button" data-toggle="dropdown">Download <span class="caret"></span></button>
 <ul class="dropdown-menu">
 	{% for v in videos %}
-	{% if v.downloadable %}	<li><a href="{{ videoprefix }}/{{v.path}}">{{ valuecheckbox(['videos',v.id,'visible'], v.visible) }} {{v.format_description}} ({{v.file_size|filesizeformat(true)}})</a></li>{% endif %}
+	{% if v.downloadable %}	<li><a href="{{ config.VIDEOPREFIX }}/{{v.path}}">{{ valuecheckbox(['videos',v.id,'visible'], v.visible) }} {{v.format_description}} ({{v.file_size|filesizeformat(true)}})</a></li>{% endif %}
 	{% endfor %}
 </ul>
 <noscript>
 	<ul class="pull-right list-unstyled" style="margin-left:10px;">
 		{% for v in videos %}
-		<li><a href="{{ videoprefix }}/{{v.path}}">{{v.format_description}} ({{v.file_size|filesizeformat(true)}})</a></li>
+		<li><a href="{{ config.VIDEOPREFIX }}/{{v.path}}">{{v.format_description}} ({{v.file_size|filesizeformat(true)}})</a></li>
 		{% endfor %}
 	</ul>
 </noscript>