cleanup template cache handling, improve tests