fix bug and tests