add test and fix bugs