fix bug and add tests