remove extra calls to ByteArrayOutputStream.toByteArray
cache Templates where possible