fixed bugs and add test