fixing template rewriting bugs