fix bug and add test code