template copy from LiuJb