refactory and fix bug