merge Qi's code