add test code and some bug fix.