fix bugs. Add test