Speedup queries

this PR attempts to help in #164 by rearranging if-statements, using better querysets, using `with` in the templates, and prefetching some things with `select_related`.