Fix core dumps while handling templates