add tests and bug fix