CFY-927 fix bug and add tests