many freemarker fixes and optimizations