add tests. Fix bugs