a bit of cleanup and made code compatable with Django 1.5+