Refactor to make more testable, added Django template