Fixed bugs found by Jianqiang Zhao.