Fix some bugs and improve tempalte