add types, tests. Fix bug