fix tests and refactor -add more type annotations