merge Qi's code and remove warnings