fix bugs and add test