fix bugs and test